function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Ankit Khurana24Ankit Khurana24 

test class for respective class:need help

 i need to write test class for the respective class...i  did like it but no coverage has been attained.

public class moveopportunity
  List<Opportunity> selectopp;
  public string campaignid1;
  public string conactid;
  List<CampaignMember> cmember;
  public moveopportunity()
   selectopp=[Select o.Id, o.CampaignId, (Select OpportunityId, ContactId From OpportunityContactRoles) From Opportunity o where Opportunity.StageName =:'Closed Lost'];
  cmember=new List<CampaignMember>();
  public void move()
            for(Opportunity O:selectopp)
                O.CampaignId = '70190000000MjHs';
                cmember=[Select c.ContactId, c.CampaignId From CampaignMember c  where CampaignId =:'70190000000MjHs'];
               for(integer i=0;i<O.OpportunityContactRoles.size();i++)
                cmember[0].ContactId= O.OpportunityContactRoles[i].ContactId ;
                update O;
                update cmember;



i wrote test class as:

private class moveopportunity_test {

    static testMethod void myUnitTest() {
        Opportunity opp = new Opportunity();
       /*8opp.Id='00690000008BBTp' ;**/
        opp.StageName ='Closed Lost' ;
        /*opp.OpportunityContactRoles.ContactId ='0039000000FtGgY';*/
        insert opp;
        moveopportunity ci = new moveopportunity();


how to modify to increase coverage to 75%


What is the current % you have ?
Ankit Khurana24Ankit Khurana24

no coverage 0%


Hi Ankit,


As read you code I think u are not following the best practices of Apex development.


Some of Point I found that.


    O.CampaignId = '70190000000MjHs'; // You shoud not use ID as hard code .


In Your test class you are writing the code




   You will get CROSS_REFERENCE_ENTITY Error. system need record Id but you are passing the Id but it will consider as string not recordId.


Try to write your code dynamic and based on best practice.


Let me know if you need any other Help



Onkar Kumar

if the class is api version 24 + then use @isTest (seealldata=true)