function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Mike DeMilleMike DeMille 

Help with Test Coverage on this Apex Class

I have the following Apex Class that I'm going to schedule to run on a daily basis:

/******************************************************************************
Name          :  BatchToUpdateAccountMarketo
Created Date  :  11/14/2017
Description   :  Batch class to update the Accounts Number_Of_Marketing_Engagements__c, 

******************************************************************************/
global class BatchToUpdateAccountMarketo implements Database.Batchable<sObject>{
   
  public static final String MarketoOwnerId = '005d0000004tpATAAY';
   
  global String queryStr;
   
  global Database.QueryLocator start(Database.BatchableContext BC){
           return Database.getQueryLocator(queryStr);
  }

  global void execute(Database.BatchableContext BC, List<Account> acctLst){
    
    //Map of original Data
    Map<Id, Account> acctMap = new Map<Id, Account>([Select Id, Number_Of_Marketing_Engagements__c
                        From Account Where Id IN: acctLst]);
                        
    Map<Id, Account> acctMapToUpdate = new Map<Id, Account>();
      Id acctId;
      List<Account> acctToUpdateLst = new List<Account>();
    Date Last90Days = Date.Today() - 90;
      
      for(Task tsk : [Select Id, CreatedDate, WhatId, AccountId
                      From Task
                      Where CreatedDate = Last_90_Days
                      AND (WhatId IN :acctLst
                      OR AccountId IN :acctLst)
                      AND (NOT Subject LIKE '%Bounce%')
                      AND (NOT Subject LIKE '%Unsubscribe%')
                      AND (NOT Subject LIKE '%Acquired%')
                      AND (NOT Subject LIKE '%Contact Purchased%')
                      AND (NOT Subject LIKE '%[Outreach][Email][Out]%')
                      AND (NOT Subject LIKE '%Email: >>%')
                      AND Owner.Id = :MarketoOwnerId]){
        
        acctId = (tsk.AccountId != null) ? tsk.AccountId : tsk.WhatId;
        
        if(!acctMapToUpdate.containsKey(acctId)){
          acctMapToUpdate.put(acctId, new Account(Id = acctId, Number_Of_Marketing_Engagements__c = 0));
        }
        
        if(tsk.CreatedDate >= Last90Days){
          acctMapToUpdate.get(acctId).Number_Of_Marketing_Engagements__c++;
        }else{
          acctMapToUpdate.get(acctId).Number_Of_Marketing_Engagements__c++;
        }
      }
      
      for(Event evt : [Select Id, CreatedDate, WhatId, AccountId
                      From Event
                      Where CreatedDate = Last_90_Days
                      AND (WhatId IN :acctLst
                      OR AccountId IN :acctLst)
                      AND (NOT Subject LIKE '%Bounce%')
                      AND (NOT Subject LIKE '%Unsubscribe%')
                      AND (NOT Subject LIKE '%Acquired%')
                      AND (NOT Subject LIKE '%Contact Purchased%')
                      AND (NOT Subject LIKE '%[Outreach][Email][Out]%')
                      AND (NOT Subject LIKE '%Email: >>%')
                      AND Owner.Id = :MarketoOwnerId]){
        
        acctId = (evt.AccountId != null) ? evt.AccountId : evt.WhatId;
        
        if(!acctMapToUpdate.containsKey(acctId)){
          acctMapToUpdate.put(acctId, new Account(Id = acctId, Number_Of_Marketing_Engagements__c = 0));
        }
        
        if(evt.CreatedDate >= Last90Days){
          acctMapToUpdate.get(acctId).Number_Of_Marketing_Engagements__c++;
        }else{
          acctMapToUpdate.get(acctId).Number_Of_Marketing_Engagements__c++;
        }
      }
      
      for(Account acct : acctMapToUpdate.values()){
        
          //update Number of Marketing Engagements This Month 
          if(acct.Number_Of_Marketing_Engagements__c != acctMap.get(acct.Id).Number_Of_Marketing_Engagements__c){
            
            //Update account only if value changes
            acctToUpdateLst.add(acct);  
              
          }
      }
      
    //update acctToUpdateLst
    if(acctToUpdateLst.size() > 0){
        if(!Test.isRunningTest()){
          update acctToUpdateLst;          
        }
    }
  }

  global void finish(Database.BatchableContext BC){

  }
}


I'm only getting 55% code coverage with the following test class:  

/******************************************************************************
Name          :  BatchToUpdateAccountMarketoTest
Created Date  :  11/14/2017
Description   :  Test Class for BatchToUpdateAccountMarketo
******************************************************************************/
@isTest
private class BatchToUpdateAccountMarketoTest {

  //For batch class BatchToUpdateAccount
  static testMethod void PopulateAccountDataTest() {
    
    User Marketo;
   
  
    
    for(User usr : [Select Id
                    From User
                    Where User.Id = '005d0000004tpATAAY'
                    AND LastName != null
                    AND IsActive = true
                    limit 1]){
      Marketo = usr;
    }
    
    
    if(Marketo != null){
      
      System.runAs(Marketo){
      
      Account testAcc = new Account(website = 'http://www.cnn.com', Name = 'test1', BillingCountry = 'US', BillingState = 'CA', BillingPostalCode = '65432',
                        OP_Named_Account__c = 'Test_OP_Named_Account__c', Company_Type__c = 'Brand');
      insert testAcc;
      
      List<Task> tskLst = new List<Task>();
      List<Event> evtLst = new List<Event>();
      
      tskLst.add(new Task(Subject = 'Test subject This Month', WhatId = testAcc.Id, ActivityDate = Date.today()));
      tskLst.add(new Task(Subject = 'Test subject Last Month', WhatId = testAcc.Id, ActivityDate = Date.today()));
      insert tskLst;
      
      evtLst.add(new Event(Subject = 'TEST EVT_THIS_MONTH # ', WhatId = testAcc.Id, DurationInMinutes = 60, ActivityDateTime = DateTime.now()));
      evtLst.add(new Event(Subject = 'TEST EVT_Last_MONTH # ', WhatId = testAcc.Id, DurationInMinutes = 60, ActivityDateTime = DateTime.now()));
      insert evtLst;
    
      Test.setCreatedDate(tskLst.get(1).Id, Date.today().addMonths(-1));
      Test.setCreatedDate(evtLst.get(1).Id, Date.today().addMonths(-1));
      }
    }
    
    Test.startTest();
    BatchToUpdateAccountMarketo AccountBatchObject = new BatchToUpdateAccountMarketo();
    AccountBatchObject.queryStr = 'Select Id from Account limit 1';
    Database.executebatch(AccountBatchObject, 100);
    Test.stopTest();
  }
  
  }

Can anyone help me figure out how to get coverage on this?  I'm a beginner and have confused myself!
Suraj TripathiSuraj Tripathi

Hi Mike,

Please add @isTest(SeeAllData=true) above to your test method that has access to all data.Like:

@isTest(SeeAllData=true)
static testMethod void PopulateAccountDataTest() {

After adding you have access all data which you have query in test class.

If it helps you marks as a best.

Regards,

Suraj