2

Test runs successfully but showing the code coverage as zero. please help.!

trigger AddCampaignMembers on Lead (after insert, after update) {
system.debug('In the trigger');
Lead[] leads = trigger.new;

/* populate complete lead details */
List<Lead> currentLeadDtls = [Select Id, Name, Owner.Id, Main_Campaign__c from lead where Id in:leads];    

/* create a "set of campaigns" and a "Map of campaign to lead" */
Set<ID> campaigns = new Set<Id>();
for (Lead l : currentLeadDtls) {
    if(l.Main_Campaign__c != null){
        campaigns.add(l.Main_Campaign__c);
    }
}

/* get all the existing leads - campaignMembers */    
List<CampaignMember> CampaignMembers = [Select CampaignId, LeadId from CampaignMember where CampaignId in:campaigns];

/* key: campaign; value: Set of Lead Ids */
Map<Id, Set<Id>> oldCampaignMemberMap = new Map<Id, Set<Id>>();
Set<Id> tempLeadIds = null;
for(CampaignMember cm : CampaignMembers){
    if(oldCampaignMemberMap.containsKey(cm.CampaignId)){
        tempLeadIds = oldCampaignMemberMap.get(cm.CampaignId);
        tempLeadIds.add(cm.LeadId);
    }else{
        tempLeadIds = new Set<Id>();
        tempLeadIds.add(cm.LeadId);
        oldCampaignMemberMap.put(cm.CampaignId, tempLeadIds);
    }        
}

Lead[] newLeads = new List<Lead>();
CampaignMember[] newCampaignMembers = new List<CampaignMember>();

for(Lead l : currentLeadDtls){
    if(l.Main_Campaign__c != null){
        //To add the lead as Campaign member 
        if(oldCampaignMemberMap.containsKey(l.Main_Campaign__c)){
            tempLeadIds = oldCampaignMemberMap.get(l.Main_Campaign__c);
            if(tempLeadIds == null || !tempLeadIds.contains(l.id)){
                CampaignMember tempCM = new CampaignMember();
                tempCM.LeadId = l.Id;
                tempCM.CampaignId = l.Main_Campaign__c;
                newCampaignMembers.add(tempCM);
           }
        }else{
            CampaignMember tempCM = new CampaignMember();
            tempCM.LeadId = l.Id;
            tempCM.CampaignId = l.Main_Campaign__c;
            newCampaignMembers.add(tempCM);
        }

    }
}

if(!newCampaignMembers.isEmpty())
    insert newCampaignMembers;

system.debug('out of the trigger');

}

@isTest
public class TestAddCampaignMembersTrigger {

    public static testMethod void testRunAs() {

        Campaign testCamp = createCampaign();
        insert testCamp;
        Lead testLead = createLead();

        Test.startTest();
            insert testLead;
            testLead.Main_Campaign__c = testCamp.Id;
            update testLead;

            /* Check for campaign members */
            List<CampaignMember> CampaignMembers = [Select CampaignId, LeadId from CampaignMember where CampaignId = :testCamp.Id ];
            Boolean check = false;
            for(CampaignMember cm : CampaignMembers){
                if(cm.LeadId == testLead.Id){
                    check = true;
                }
            }
            system.assertEquals(true, check);
        Test.stopTest();        
    }

    public static Campaign createCampaign(){
        Campaign testCamp = new Campaign();
        testCamp.Name = 'testCamp';
        testCamp.isActive = true;
        return testCamp;
    }

    public static Lead createLead(){
        Lead testLead = new Lead();
        testLead.lastName = 'testLastName';
        testLead.Company = 'testcompany';
        return testLead;
    }

}
  • can you post the trigger code as well? – Goki Dec 1 '15 at 22:10
  • In all probability, it's probably just a glitch. Try clearing your coverage and then running all tests. See, by way of example, salesforce.stackexchange.com/questions/73575 and salesforce.stackexchange.com/questions/81297 for two related questions that may help you. – sfdcfox Dec 1 '15 at 22:22
  • Having just refreshed and looked at your trigger code, I can guarantee that your coverage is not 0% if the test is running successfully. Try clearing your cache and running all your tests from scratch. See above linked questions on how to do this. – sfdcfox Dec 1 '15 at 22:23
  • Cleared the cache as mentioned in the post, but still no luck.. Getting 0% coverage! – Kusuma Dec 1 '15 at 22:42
3

I recently had the same problem, and as far as I could tell it came down to a bug related to running tests from the Developer Console. Try this work around: run the tests from the main interface by going to Setup > Develop > Apex Test Execution and run your test from there. If you are having the same issue I was seeing, you should see the correct test coverage when done that way.

EDIT: Its a known issue: https://success.salesforce.com/issues_view?id=a1p30000000eMoyAAE

Winter '16 - In Developer Console, running tests synchronously does not generate code coverage
Apex , Developer Console , Winter 16

Summary
In Winter '16, Salesforce introduced a new option under the 'Test' tab in the Developer Console called 'Always Run Asynchronously'. By default, this option is unchecked which will in turn run tests synchronously.

Presently, in Winter '16 orgs, when we run tests synchronously via the Developer Console, we observe no code coverage is calculated. Further, when we query the objects 'ApexCodeCoverage' and 'ApexCodeCoverageAggregate' via Tooling API, we observe no results for the class being tested.

Repro 1. In a Winter '16 org, create a new trigger as follows using any API version:

trigger codeCoverageTrigger on Case (before insert) { 
    for (Case c: Trigger.new)  {  
        c.Subject='TEST CASE for code coverage';
        c.description = 'Hello World';  
    }
}
  1. Create a test class for the trigger to generate code coverage as follows:
@isTest  public class codeCoverageTrigger_Test { 
    static testMethod void insertCase() {
        Case testCase = new Case(); 
        testCase.Origin='Web';  
        testCase.Status='New';  
        insert testCase ;
    } 
}
  1. Open Developer Console and examine the 'Test' tab, observe the option 'Always Run Asynchronously'. In Winter '16 orgs, this option is unchecked by default which will in turn run tests synchronously. For this repro, leave it unchecked.

  2. In Developer Console, open the test class 'codeCoverageTrigger_Test' (Developer Console->File->Open->Classes) and click 'Run Test'

  3. You can monitor the test run via 'Apex Test Execution' section(Setup->Develop->Apex Test Execution), once complete open the trigger 'codeCoverageTrigger'

  4. Observe the code coverage for the trigger is 0%, if we further examine the code coverage breakdown via Developer Console or by querying the objects 'ApexCodeCoverage' and 'ApexCodeCoverageAggregate' via Tooling API, we will observe no coverage is generated for the trigger

Workaround:

Workaround In Winter '16 orgs, if we check the option 'Always Run Asynchronously' under the 'Test' tab and re-run the test class via Developer Console, code coverage will be generated as expected.

Alternatively, if the tests are ran via the UI, code coverage is generated as expected.

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.