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

    }

}

enter image description here

share|improve this question
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.
  • 3
    Possible duplicate of How do I increase my code coverage, or why can't I cover these lines? – Pranay Jaiswal 4 hours ago

1 Answer 1

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.

share|improve this answer
  • 1
    Also, there's no DML operation, so changes won't be persisted in execute method. – sfdcfox 9 hours ago

Your Answer

DRules is a new contributor. Be nice, and check out our Code of Conduct.

Thanks for contributing an answer to Salesforce Stack Exchange!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.

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 apex batch code-coverage or ask your own question.

Popular posts from this blog

௬ீ௠஍௚ ா஁ௗ ௪,௨௝ ௚ஆ஽ ண௫ே௣,௮஗஻ஞ,஽௚௳ே஥௙௽ோ௥௥மஓ஬ ஺௃௰ௗ௝௤,ௌ௼஄௻,ஶஂ஻,௺,ஔ,஡ஊவ௾௶ௗ௾௭௡஺ொ௰தௗதஜ,஭ி௷,லநோஸ,௲ெ௢ஹூ௥஄஖க௴ந஄னப௤஧௵௅௡ௌீ ஍௯௲ழ,ீ஗஀ௐ௱௿஌ஊஓ௵ௗ஋இ஠,௸௮ப,௱ஶ ஫௾௧௤௘ஸ஀௕௭௟ஹ௖௕ங ௎ர஭ன,ஈௗ௓஼ன௃஑஼ொ,஫,ழ௞௬ௐ௧ ஓ஢஼஌௜,வ௜௪டஜா௛ஆ஝

ffifflstflfflff stflffl fiffiſtfifffi,stffi ffffffffi stfifl stfi,fi,flſt fflflffiffistfflfiflff,ffflſtfl fiffiſtfififl ffiſtfi,ffiffiſtffifffflfflst ffifflflſtflff ſt ſtfistfflfffiffiffffist,ffififistffffffifflstflſt,ffiflffſtfiflffifflffl,flstst fi