Batch Test class not calling execute methods3;)1bmor 7—
0
I have created below batch apex class. I have also created test class for this. But this test class is only covering 27% of batch apex class. Also, it is not calling execute method of batch apex class.
Batch Apex class:
global class LeadDuplicateRemove implements Database.Batchable < sObject > {
global Database.QueryLocator start(Database.BatchableContext bc) {
return Database.getQueryLocator('select Id, CreatedDate, Phone, Status, Email from Lead Where convertedbusiness__c != null and LeadAudit__c = null and isDeleted = false order by CreatedDate ASC Limit 1');
}
global void execute(Database.BatchableContext bc, List < Lead > scope) {
for (Lead lead: scope) {
Database.QueryLocator leadQueryLocator = Database.getQueryLocator([Select Id, CreatedDate, Phone, Status, Email from Lead where convertedbusiness__c != null and LeadAudit__c = '1' and isDeleted = false order by CreatedDate ASC Limit 1]);
Database.QueryLocatorIterator iterator = leadQueryLocator.iterator();
while (iterator.hasNext()) {
Lead leadObj = (Lead) iterator.next();
if(lead.ConvertedBusiness__c == leadObj.ConvertedBusiness__c){
lead.ConvertedBusiness__c = null;
}
}
}
}
global void finish(Database.BatchableContext bc) {
}
}
Test class:
@isTest global class LeadDuplicateRemoveTest {
@testSetup
global static void setup() {
Lead lead1 = new Lead(LastName='testlead1 ', RAF_Nurture_Campaign__c = 'None',
company='test', Email='testemai@email.com.invalid',Phone = '1111111111',Status ='Completed');
insert lead1;
insert new Lead(LastName='testlead1', RAF_Nurture_Campaign__c = 'None',
company='Vtest', Email='testemai@email.com.invalid',Phone = '222222',Status ='Open');
}
@isTest global static void TestRun() {
Test.startTest();
Database.executeBatch(new LeadDuplicateRemove());
Test.stopTest();
}
}

apex batch code-coverage
New contributor
DRules is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
-
3Possible duplicate of How do I increase my code coverage, or why can't I cover these lines? – Pranay Jaiswal 4 hours ago
add a comment |
1 Answer
active
oldest
votes
3
You have the filter convertedbusiness__c != null but when you are creating the lead, you are not setting this.
Lead lead1 = new Lead(convertedbusiness__c= ??, LastName='testlead1 ', RAF_Nurture_Campaign__c = 'None',
company='test', Email='testemai@email.com.invalid',Phone = '1111111111',Status ='Completed');
As no records are returned from start method, it is not even invoking execute method
Note:
Also, you have query locator and iterator inside execute - which is not needed. You are returning leads where LeadAudit__c = null and again processing on records where LeadAudit__c = '1'. You might as well remove the filter LeadAudit__c = null from start method and process the records inside execute without iterator.
-
1Also, there's no DML operation, so changes won't be persisted in execute method. – sfdcfox 9 hours ago
add a comment |