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
cjpradcjprad 

Code Coverage Help

Hi, I need assistance in providing code coverage for the below code in bold.

Apex Class:
public class AddListToCampaignFlowAction {
                
       @InvocableMethod(label='Add List to Campaign Apex Action' description='Provide List Id and Campaign Id')
       public static void PassListIdApexMethod(AddListToCampaignApexActionRequest[] requests) { 

          if(requests.size() == 0) { return; }
          else {
              Id ListId = requests[0].ListId;
              Id CampaignId = requests[0].CampaignId;
              List<CampaignMember> existingCampMembers = new List<CampaignMember>();
              existingCampMembers = [SELECT Id, ContactId FROM CampaignMember WHERE CampaignId=:CampaignId];
              List<Contact> existingContacts = new List<Contact>();
              for(CampaignMember cm : existingCampMembers ) {
                  Contact con = new Contact();
                  con.Id = cm.ContactId;
                  existingContacts.add(con);

              }
              Map<Id,Contact> existingContactsMap = new Map<Id,Contact>(existingContacts);

              List<CampaignMember> campmembers = new List<CampaignMember>();
              List<WVU_PO_List_Affiliation__c> listAffiliations = new List<WVU_PO_List_Affiliation__c>();
              listAffiliations = [SELECT Id, WVU_PO_Contact__c FROM WVU_PO_List_Affiliation__c WHERE WVU_PO_List__c=:ListId AND Contact_Active__c = TRUE];
                        
              for(WVU_PO_List_Affiliation__c listAffiliation : listAffiliations ) {
                if(existingContactsMap.containsKey(listAffiliation.WVU_PO_Contact__c)) {continue;}
                else {
                    CampaignMember campmem = new CampaignMember();
                    campmem.CampaignId = CampaignId;
                    campmem.ContactId = listAffiliation.WVU_PO_Contact__c;
                    campmem.Status = 'Invited';
                    campmembers.add(campmem);

                }
              }                
              insert campmembers;
          }
      }
      
      public class AddListToCampaignApexActionRequest {
          
          @InvocableVariable(required=true)
          public Id ListId;
          
          @InvocableVariable(required=true)
          public Id CampaignId;
          
      }
}

Test Class
@isTest(SeeAllData=true)

public class AddListToCampaignFlowActionTest {
    
    static testMethod void testAddListToCampaignFlowAction() {
        
        //Create test contact
        Contact con = new Contact();
        con.LastName = 'TestListCampaign';
        insert con;
        Contact testCon = [SELECT Id, LastName FROM Contact WHERE LastName = 'TestListCampaign' ORDER BY LastModifiedDate DESC LIMIT 1];

        //Create test list
        WVUPO_List__c tList = new WVUPO_List__c();
        tList.Name = 'TestList';
        insert tList;
        WVUPO_List__c testList = [SELECT Id, Name FROM WVUPO_List__c WHERE Name = 'TestList' ORDER BY LastModifiedDate DESC LIMIT 1];
        
        //Create test list affiliation
        WVU_PO_List_Affiliation__c tListAff = new WVU_PO_List_Affiliation__c();
        tListAff.WVU_PO_Contact__c = testCon.Id;
        tListAff.WVU_PO_List__c = testList.Id;
        insert tListAff;
        WVU_PO_List_Affiliation__c testListAff = [SELECT Id FROM WVU_PO_List_Affiliation__c WHERE WVU_PO_Contact__c =:testCon.Id AND WVU_PO_List__c =:testList.Id ORDER BY LastModifiedDate DESC LIMIT 1];
        
        //Create test campaign
        Campaign camp = new Campaign();
        camp.Name = 'TestCampaign';
        insert camp;
        Campaign testCamp = [SELECT Id, Name FROM Campaign WHERE Name = 'TestCampaign' ORDER BY LastModifiedDate DESC LIMIT 1];
        
        //Create array of requests
        AddListToCampaignFlowAction.AddListToCampaignApexActionRequest testRequest = new AddListToCampaignFlowAction.AddListToCampaignApexActionRequest();
        List<AddListToCampaignFlowAction.AddListToCampaignApexActionRequest> testRequests = new List<AddListToCampaignFlowAction.AddListToCampaignApexActionRequest>();
        testRequest.ListId = testList.Id;
        testRequest.CampaignId = testCamp.Id;
        testRequests.add(testRequest);
        
        //Start test
        //Test.startTest();
        AddListToCampaignFlowAction.PassListIdApexMethod(testRequests);
        
        //Test.stopTest();
    }
}
Raj VakatiRaj Vakati
try this
@isTest(SeeAllData=false)

public class AddListToCampaignFlowActionTest {
    
    static testMethod void testAddListToCampaignFlowAction() {
        
        //Create test contact
        Contact con = new Contact();
        con.LastName = 'TestListCampaign';
        insert con;
        Contact testCon = [SELECT Id, LastName FROM Contact WHERE LastName = 'TestListCampaign' ORDER BY LastModifiedDate DESC LIMIT 1];

        //Create test list
        WVUPO_List__c tList = new WVUPO_List__c();
        tList.Name = 'TestList';
        insert tList;
        WVUPO_List__c testList = [SELECT Id, Name FROM WVUPO_List__c WHERE Name = 'TestList' ORDER BY LastModifiedDate DESC LIMIT 1];
        
        //Create test list affiliation
        WVU_PO_List_Affiliation__c tListAff = new WVU_PO_List_Affiliation__c();
        tListAff.WVU_PO_Contact__c = testCon.Id;
        tListAff.WVU_PO_List__c = testList.Id;
        insert tListAff;
		
		  WVU_PO_List_Affiliation__c tListAff1 = new WVU_PO_List_Affiliation__c();
        tListAff1.WVU_PO_Contact__c = testCon.Id;
		tListAff1.Contact_Active__c =true ; 
        tListAff1.WVU_PO_List__c = testList.Id;
        insert tListAff1;
      
	  
	  
        WVU_PO_List_Affiliation__c testListAff = [SELECT Id FROM WVU_PO_List_Affiliation__c WHERE WVU_PO_Contact__c =:testCon.Id AND WVU_PO_List__c =:testList.Id ORDER BY LastModifiedDate DESC LIMIT 1];
        
        //Create test campaign
        Campaign camp = new Campaign();
        camp.Name = 'TestCampaign';
        insert camp;
        Campaign testCamp = [SELECT Id, Name FROM Campaign WHERE Name = 'TestCampaign' ORDER BY LastModifiedDate DESC LIMIT 1];
        
		 CampaignMember member = new CampaignMember(
        ContactId = con.Id,
        Status = 'Completed',
        CampaignId = camp.Id
        ); 
    insert member; 
	
        //Create array of requests
        AddListToCampaignFlowAction.AddListToCampaignApexActionRequest testRequest = new AddListToCampaignFlowAction.AddListToCampaignApexActionRequest();
        List<AddListToCampaignFlowAction.AddListToCampaignApexActionRequest> testRequests = new List<AddListToCampaignFlowAction.AddListToCampaignApexActionRequest>();
        testRequest.ListId = testList.Id;
        testRequest.CampaignId = testCamp.Id;
        testRequests.add(testRequest);
        
        //Start test
        //Test.startTest();
        AddListToCampaignFlowAction.PassListIdApexMethod(testRequests);
        
        //Test.stopTest();
    }
}

 
cjpradcjprad

Thanks @Raj, I was able to improve my coverage with your code by inserting campaign members. However, I am still at 82% coverage. This part of apex class is still not covered.

 if(existingContactsMap.containsKey(listAffiliation.WVU_PO_Contact__c)) {continue;}
                else {
                    CampaignMember campmem = new CampaignMember();
                    campmem.CampaignId = CampaignId;
                    campmem.ContactId = listAffiliation.WVU_PO_Contact__c;
                    campmem.Status = 'Invited';
                    campmembers.add(campmem);

 

Raj VakatiRaj Vakati
Its very easy .. 

insert WVU_PO_List_Affiliation__c with out WVU_PO_Contact__c and  pass it to the wrapper class