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
JN22JN22 

Increase Test Coverage for Trigger

Hello,

I have a trigger on my campaign object that updates member statuses.  The trigger works fine, but my test class is only generating 64% coverage.  The test class is not covering lines 19-26 .  Does anyone know how I can increase coverage in my test class to at least get above the 75% threshold?  Thanks.

Trigger:
trigger CampaignMemberStatus on Campaign (after insert) {

if(checkRecursiveAI.runOnceAI())
{
   
    List<Campaign> newCamps = [select Id from Campaign where Id IN :trigger.new AND ParentID = Null];
    List<CampaignMemberStatus> cms = new List<CampaignMemberStatus>();
    Set<Id> camps = new Set<Id>();
    List<CampaignMemberStatus> cms2Delete = new List<CampaignMemberStatus>();
    List<CampaignMemberStatus> cms2Insert = new List<CampaignMemberStatus>();
   
    for(Campaign camp : newCamps){
      
            camps.add(camp.Id);
    }  
    
   for (CampaignMemberStatus cm: [Select Id, Label, CampaignID  FROM CampaignMemberStatus WHERE CampaignID IN :camps]){
      if(cm.Label == 'Responded' ){
            CampaignMemberStatus cms1 = new CampaignMemberStatus(CampaignId=cm.CampaignID, Label='Registered', HasResponded=false, IsDefault = False, SortOrder=4);          
            System.debug(cms1);
            cms2Delete.add(cm);
            cms2Insert.add(cms1);
           
            CampaignMemberStatus cms3 = new CampaignMemberStatus(CampaignId = cm.CampaignId, HasResponded=true, Label = 'Attended', SortOrder = 5);
            cms2Insert.add(cms3);

      }
   }
    //perform insert before delete because system requires at least one CMS for a Campaign
    insert cms2Insert;
    delete cms2Delete;

}
}

Test Class:
@isTest private class CampaignMembStatus{

    @isTest private static void test_Memb_Status() {
    
        Campaign camp=new Campaign(Name='Test Campaign',IsActive=True);
        Insert camp;

        Account acct = new Account(name='Test Account', Type = 'Employer');
        insert acct;

        Contact cont = new Contact(FirstName='Test',LastName='Contact',AccountId=acct.Id);
        insert cont;
 
        CampaignMember cMemb = new CampaignMember(CampaignId=camp.Id, ContactId=cont.Id,Status='Responded');
        Insert cMemb ; 

        CampaignMemberStatus cMembStat = new CampaignMemberStatus(CampaignId=camp.Id, Label='Responded',HasResponded=false,IsDefault=False,SortOrder=4);
        Insert cMembStat ; 

        Test.StartTest();
        update cMembStat;
        Test.StopTest();
        
    }
}



pconpcon
Your test class will never fire your trigger.  Since you have startTest and endTest around the update operation and your trigger is for insert, it will never occur.  You will want to create a test case that tests the insert of your object.  one with a status of 'Responded' and at least one without.  And it best practice to actually assert that the expected changes actually occurred in your code.
JN22JN22
Thanks.  I've never created a test for insert.  Would moving my Test.StartTest line to before Line 14 be how that is done?  Thanks.
pconpcon
Sorta.  But for testing this trigger, you will not need the update.  I would recommend moving the test.startTest and stopTest around the insert of your campain.  You will need to already have your CampainMemberStatus inserted to make sure it hits that part of trigger.  Then you'll want to query your CampainMemberStatus records to make sure they have been inserted/deleted correctly.
JN22JN22
Thanks.  I'm a little confused.  Don't I need to insert the campaign before I insert the CampaignMemberStatus values?
pconpcon
Actually, looking at your trigger, that code will NEVER execute.  Since this is an insert trigger, the campaign does not exist.  So therefore there can be no CampaignMemberStatus objects associated with it.  Because of this you'll want to make this an after update trigger.  It may be better to ask what you are trying to do.  You've got a lot of opimization you could save on this trigger.  Lots of SOQL that you do not need.  Any reason why you are deleting and inserting instead of updating the CampaignMemberStatus?
JN22JN22
The trigger is meant to fire when a campaign is created and change the deafult members status of "Registered" to "Responded", while also adding a new status of "Attended".