1

I have a trigger that I only have 92% test coverage for and when I review the lines not covered using the Developer Console, it is only the two lines "continue;". How do I incorporate these into my test class so I can get the highest coverage possible?

Trigger

trigger SetPrimaryCampaign on Opportunity (after insert) {                   

     // Make a List of Opps and Contact ID's.                        
    List<Id> oppIds = new List<Id>();
    List<Id> contIds = new List<Id>();                    

    for( Opportunity o : Trigger.new ) {                    
        if(o.Sourced_by__c <> null)  {                  
             // If it is determined this should fire on update too. You will want the line below to ensure this does not fire on every update only when the Sourced_by__c value changes                    
             // && (Trigger.oldMap.get(opp.Id).Sourced_by__c != opp.Sourced_by__c ) )                   
                oppIds.add( o.Id ); 
                contIds.add( o.Sourced_by__c );                 
                continue;                   
        }                   
    }                   

    Map<Id,Opportunity> oppMap = new Map<Id,Opportunity>( [SELECT Id, Sourced_by__c from Opportunity where Id IN :oppIds] );                    
    Map<Id, CampaignMember> campMap = new Map<Id, CampaignMember>([Select id, ContactId, CampaignId, CreatedDate From CampaignMember where ContactId IN : contIds]);                 

    List<Opportunity> opps = new List<Opportunity>();                   
        for( Opportunity opp : oppMap.values() ) {              
            Opportunity op = new Opportunity();         

                CampaignMember oldestCM; //Carries the ID of the oldest CampaignMember by CreatedDate       
                Datetime oldestdate; //Holds the date for the oldestCM value that is checked        

                for(CampaignMember cammem : campMap.values() ) {        
                    if((cammem.CreatedDate <= oldestdate || oldestdate == null) && cammem.ContactId == opp.Sourced_by__c){  
                        oldestCM = cammem;
                        oldestdate = cammem.CreatedDate;
                    }   
                    else{   
                        continue;
                    }   
                }       
                    if(oldestCM <> null){   
                        op.id = opp.id;
                        op.CampaignId = oldestCM.campaignId;
                        opps.add(op);
                    }   
                    else{   
                        continue;
                    }   
        }               
        update opps;                    
    }

Test Class

@IsTest (SeeAllData=true) private class VennSci_TEST_SetPrimaryCampaign{

    private static testMethod void myUnitTest() {

// create an Account
Account acc=new Account(Name='test');
insert acc;                

 // create a Contact
        Contact cont = new Contact(
        Firstname='Jane',
        Lastname='Doe',
        AccountID= acc.Id

    );
    insert cont;

       // create the 1st Campaign
    Campaign c1 = new Campaign(
        name='Campaign 1',
        IsActive=true,
        StartDate=Date.newInstance(2013,01,01)
        );
    insert c1;

   // create the 2nd Campaign
    Campaign c2 = new Campaign(
        name='Campaign 2',
        IsActive=true,
        StartDate=Date.newInstance(2014,01,01)
        );
    insert c2;

    // create the ist Campaign Member
    Campaignmember cm1 = new CampaignMember(
        CampaignId=c1.Id,
        ContactID=cont.Id,
        Status='Sent'
             );
    insert cm1;

   // create the 2nd Campaign Member
    CampaignMember cm2 = new CampaignMember(
        CampaignId=c2.Id,
        ContactId=cont.Id,
        Status='Sent'
                );
    insert cm2;


    // create the opportunity positive
        Opportunity opp1 = new Opportunity(
        name='Test Opp 1',
        StageName = 'Closed Won',
        CloseDate = Date.newInstance(2014,01,01),
        Sourced_By__c = cont.Id 

    );
    insert opp1;


    // create the opportunity negative
        Opportunity opp2 = new Opportunity(
        name='Test Opp 2',
        StageName = 'Closed Won',
        CloseDate = Date.newInstance(2014,01,01)

    );
    insert opp2;


   }
   }
3

You can remove both of the continue; lines from your code. They serve no real purpose because there is no following code to skip over. The same would apply to the 2nd else statement.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.