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){

/* 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){
        tempLeadIds = oldCampaignMemberMap.get(cm.CampaignId);
        tempLeadIds = new Set<Id>();
        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 
            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;
            CampaignMember tempCM = new CampaignMember();
            tempCM.LeadId = l.Id;
            tempCM.CampaignId = l.Main_Campaign__c;


    insert newCampaignMembers;

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


public class TestAddCampaignMembersTrigger {

    public static testMethod void testRunAs() {

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

            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);

    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

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

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(); 
        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 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.