+ Start a Discussion
Haseeb Ahmad 9Haseeb Ahmad 9 

No code coverage issue

Hi Everyone,

Can someone help me figure out why this class not getting any code coverage?
 
public class CampaignSourceBatch implements database.Batchable<sObject>, database.stateful {
    
    private final String query;
    private String errorMessage = '';    
    
    // custom query
    public CampaignSourceBatch(String query) {
        this.query = query;
    }

    public Database.QueryLocator start(Database.BatchableContext bc) {
        return Database.getQueryLocator(query);
    }
    
    public void execute(Database.BatchableContext bc, List<sObject> parentRecords) {
        DiagnosticService.push('CampaignSourceBatch.execute');
        DiagnosticService.debug('parentRecords: ' + parentRecords);
        
        // error for single batch
		String batchError = '';
		        
        CampaignSourceService service = new CampaignSourceService();
        Set<Id> parentIds = new Map<Id, sObject>(parentRecords).keySet();
        batchError += service.setMQLCampaignSource(parentIds);
        batchError += service.setPrimaryCampaignSource(parentIds);
        
        if (!String.isBlank(batchError)) {
            DiagnosticService.debug('batchError: ' + batchError);     
            errorMessage += batchError;
        }
        DiagnosticService.pop();
    }
    
    public void finish(Database.BatchableContext bc) {
        DiagnosticService.push('CampaignSourceBatch.finish');        
        if (!String.isBlank(errorMessage)) {
            DiagnosticService.debug('errorMessage: ' + errorMessage);            
			String subject = 'Error(s) in CampaignSourceBatch ' + Date.today();
            Utils.sendEmailToAdmin(subject, errorMessage);
        }
        DiagnosticService.pop();
    }
}

Test Class:
 
@isTest
public class CampaignSourceServiceTest {
	@testSetup
    public static void setup() {
        
        // create leads / contacts with status = 'Open'
		List<Lead> leads = new List<Lead>();
        List<Contact> contacts = new List<Contact>();
        for (Integer i=0; i<10; i++) {
            leads.add(new Lead(lastName = 'test lead ' + i, email = 'testLead' + i + '@test.com', company = 'test ' + i,  mql_date__c = Datetime.now()));
            contacts.add(new Contact(lastName = 'test contact ' + i, email = 'testContact' + i + '@test.com', mql_date__c = Datetime.now()));
        }
        
        insert leads;
        insert contacts;
        
        List<Campaign> campaigns = new List<Campaign>();
        campaigns.add(new Campaign(name = 'campaign 1'));
        campaigns.add(new Campaign(name = 'campaign 2'));
        campaigns.add(new Campaign(name = 'campaign 3'));
        insert campaigns;
    }
    
    public static testMethod void testCampaignMember_Insert() {
        // retrieve campaigns
   		List<Campaign> campaigns = getCampaigns();
        Campaign c1 = campaigns[0];
        campaign c2 = campaigns[1];
        campaign c3 = campaigns[2];

        // retrieve leads and contacts
        List<Lead> leads = getLeads().values();
        List<Contact> contacts = getContacts().values();        
        
        // create campaign members for each parent record, using the first two campaigns        
        List<CampaignMember> campaignMembers = new List<CampaignMember>();
        for (Lead lead : leads) {
            campaignMembers.add(new CampaignMember(leadId = lead.Id, campaignId = c1.id, status = 'Sent'));
            campaignMembers.add(new CampaignMember(leadId = lead.Id, campaignId = c2.id, status = 'Responded'));            
        }
        for (Contact contact : contacts) {
            campaignMembers.add(new CampaignMember(contactId = contact.Id, campaignId = c1.id, status = 'Sent'));
            campaignMembers.add(new CampaignMember(contactId = contact.Id, campaignId = c2.id, status = 'Responded'));            
        }   
        insert campaignMembers;
        
        
        // assert that the campaign source values were set appropriately 
        // - mql is the first campaign after the mql date on the parent
        // - primary is the most recent 'responded' campaign
        for (Lead lead : getLeads().values()) {
            system.assertEquals(c1.id, lead.mql_campaign_source__c);
            system.assertEquals(c2.id, lead.Primary_Campaign_Source__c);
        }
        
        for (Contact contact : getContacts().values()) {
            system.assertEquals(c1.id, contact.mql_campaign_source__c);
            system.assertEquals(c2.id, contact.Primary_Campaign_Source__c);
        }      
        
        // add third campaign membership as 'responded' to ensure that the MQL Campaign Source stays the same, and that the Primary Campaign Source changes
        campaignMembers.clear();
        for (Lead lead : leads) {
            campaignMembers.add(new CampaignMember(leadId = lead.Id, campaignId = c3.id, status = 'Responded'));            
        }
        for (Contact contact : contacts) {
            campaignMembers.add(new CampaignMember(contactId = contact.Id, campaignId = c3.id, status = 'Responded'));            
        }   
        
        Test.startTest();
        insert campaignMembers;
        Test.stopTest();
        
        // assert that MQL source stayed the same, but that Primary source updated
        for (Lead lead : getLeads().values()) {
            system.assertEquals(c1.id, lead.mql_campaign_source__c);
            system.assertEquals(c3.id, lead.Primary_Campaign_Source__c);
        }
        
        for (Contact contact : getContacts().values()) {
            system.assertEquals(c1.id, contact.mql_campaign_source__c);
            system.assertEquals(c3.id, contact.Primary_Campaign_Source__c);
        }          
    }
    
    // NEED TO REVIEW TEST FAILURES - only fails during deployment in production 
    public static testMethod void testCampaignMemberUpdate() {
        // retrieve campaigns
   		List<Campaign> campaigns = getCampaigns();
        Campaign c1 = campaigns[0];
        campaign c2 = campaigns[1];

        // retrieve leads and contacts
        List<Lead> leads = getLeads().values();
        List<Contact> contacts = getContacts().values();        
        
        // create campaign members for each parent record, using the first two campaigns        
        List<CampaignMember> campaignMembers = new List<CampaignMember>();
        for (Lead lead : leads) {
            campaignMembers.add(new CampaignMember(leadId = lead.Id, campaignId = c1.id, status = 'Sent'));
            campaignMembers.add(new CampaignMember(leadId = lead.Id, campaignId = c2.id, status = 'Responded'));            
        }
        for (Contact contact : contacts) {
            campaignMembers.add(new CampaignMember(contactId = contact.Id, campaignId = c1.id, status = 'Sent'));
            campaignMembers.add(new CampaignMember(contactId = contact.Id, campaignId = c2.id, status = 'Responded'));            
        }   
        insert campaignMembers;
  
        // flip all 'c1' campaign members to 'Responded'
        for (CampaignMember member : campaignMembers) {
            if (member.campaignId == c1.id) {
                member.status = 'Responded';
            }
        }        
        
        Test.startTest();
        update campaignmembers;
        Test.stopTest();
        
        // assert that primary source updated since the c1 membership changed to 'responded'
        for (Lead lead : getLeads().values()) {
            system.assertEquals(c1.id, lead.Primary_Campaign_Source__c);
        }
        
        for (Contact contact : getContacts().values()) {
            system.assertEquals(c1.id, contact.Primary_Campaign_Source__c);
        }          
    }
	
    
    private static List<Campaign> getCampaigns() {
        return [select id from campaign];
    }
    
    private static Map<Id, Lead> getLeads() {
        return new Map<Id, Lead>([select id, mql_campaign_source__c, primary_campaign_source__c from lead]);
    }
    
    private static Map<Id, Contact> getContacts() {
        return new Map<Id, Contact>([select id, mql_campaign_source__c, primary_campaign_source__c from contact]);
    }
}

Thank you for your help. ​​​​​​​