+ Start a Discussion
Maharajan CMaharajan C 

Test Code Coverage

Hi All,

Any One  please help me to code coverge because i got only 43% coverage

ApexTrigger :-
trigger sendNotificationTrigger on CampaignMember (after insert) {
    Set<Id> LeadIds = new Set<ID>();
    Lead_Campaign__c tm;//Assinging Custom setting To the variable tm
    String Template=tm.Email_Template_ID__c;
    Decimal Days=tm.Threshold_Days__c;
    list <CampaignMember> theCampaignMembers = new list<CampaignMember>();
    for(CampaignMember campMem : Trigger.new){//
        Days = 0;
        if(campMem.leadid != null){
    // List containing Campaign Member records to be inserted  
    List<Messaging.SingleEmailMessage> mails =new List<Messaging.SingleEmailMessage>();   
    for(Lead ld : [select id, Status,Lead_age__c, owner.email from Lead where id IN : LeadIds])

    //Checking Condition Status not equal to Qualified and Lead_Age_In_days__c greater than equal to 30
     Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();// For Email
      List<String> sendTo = new List<String>();
      sendTo.add(ld.Owner.Email);//sending message via Email to the Owner of the lead
      mail.saveAsActivity = false;
      mail.setTemplateId(Template);//Using custom setting field template as template id
 catch (Exception e)

  Profile adminProfile = [Select id From Profile Where Name='System Administrator' Limit 1];

     Messaging.SingleEmailMessage mail=new Messaging.SingleEmailMessage();
     List<String> toAddresses = new List<String>();
     mail.setSenderDisplayName('Apex error message');
     mail.setSubject('Error from Org : ' + UserInfo.getOrganizationName());
     Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });

Test Class -:

@isTest(SeeAllData = true)
public class sendNotificationTrigger
static testMethod void sendNotificationTrigger ()
     Test.startTest();      //Creates Contact to be linked to Campaign Member
Campaign cp =  [SELECT Id FROM Campaign LIMIT 1];
       //Creates a new campaign memeber, associaites it with 1 campaign
 Lead t1 = new Lead(Company= 'TestLead', LastName= 'TestL', Email = 'none@test.com',Status = 'Open' );
 insert t1;
 CampaignMember newMember = new CampaignMember (LeadId = t1.id, status='Sent', campaignid = cp.id);
 insert newMember;
 system.assertequals(t1.status,'Open')  ;



You have written the code in Catch block so you need to throw an exception in try block so that your code will come in catch block and cover the code.

You can use below  this line after  Messaging.sendEmail(mails); in try block so the code get exception and come in Catch.

if(test.isRunningTest()){  Days = 1/0;}

And according to documentation only 1% code required for Trigger.

Ashlekh Gera
Maharajan CMaharajan C
Hi Ashiekh,

Sry Thats Not Work

Thanks For Your Reply

Run test from developer console and then use overall code coverage section to locate the lines of code that are not covered. SF highlights those lines immediately after you have run tests on those classes from developer console.
Marek Kosar_Marek Kosar_
Hi Maharaja,

first of all, you shouldn't use (SeeAllData = true) annotation, it's a very bad practice (for example - this particular test case will fail if you try to deploy it to clean org, because of absence of any Campaign there).
So you should create all test date in test class, always.

Then, you're doing this: "ld.Lead_age__c>=Days", but lead age filed is null, because you didn't assign it in your test class, what is probably throwing exception -> so you are covering "catch block".
-> Try to create another test method, copypasted previous one and assign lead age field with some value (>0).

Lars NielsenLars Nielsen
Great point Marek on the SeeAllData = true

That got me into trouble when I first started...