Skip to main content
Hello,

 

In User Batch Apex challenge of Asynchronous Apex, I am facing issue of code coverage. After making all the changes, I can achieve only upto 37% code coverage. Could anyone please let me know where I am wrong.

 

Thanks in Advance..

 

 

1. Main Class

public class LeadProcessor implements Database.Batchable<sObject> {

public Database.QueryLocator start(Database.BatchableContext bc) {

return Database.getQueryLocator(

[SELECT Id, Name FROM Lead]

);

}

public void execute(Database.BatchableContext bc, List<Lead> leads){

for(Lead l: leads){

l.LeadSource = 'Dreamforce';

}

update leads;

}

public void finish(Database.BatchableContext bc){

System.debug('Done');

}

}

2. Test Class

@isTest

private class LeadProcessorTest {

@isTest

private static void testLeadProcessorBatch(){

// Load Test Data

List<Lead> leads = new List<Lead>();

for(Integer i=0;i<=200;i++){

leads.add(new Lead(LastName='LeadLstName', Company='SalesForce'));

}

insert leads;

// perfor test

Test.startTest();

LeadProcessor lp = new LeadProcessor();

Id batchId = Database.executeBatch(lp,200);

Test.stopTest();

//check result

List<Lead> updatedLeads = [SELECT Id FROM Lead WHERE LeadSource='Dreamforce'];

System.assertEquals(200, updatedLeads.size(),'ERROR: at least 1 Lead record not updated correctly');

}

}

 

 
답변 7개
  1. 2021년 6월 2일 오전 11:50
    Hi Videen,

     

    "Try this it will give you 100% coverage"

     

    Apex Class

    public class LeadProcessor implements Database.Batchable<sObject> {

        

            public Database.QueryLocator start(Database.BatchableContext bc) {

                return Database.getQueryLocator(

                    [SELECT Id, Name FROM Lead]

                );

            }

            public void execute(Database.BatchableContext bc, List<Lead> leads){

                for(Lead l: leads){

                     l.LeadSource = 'Dreamforce';

                }

                update leads;

            }

        

            public void finish(Database.BatchableContext bc){

                System.debug('Done');       

            }

        }

     

    Test Class

     

     

    @isTest

    private class LeadProcessorTest {

        @isTest

        private static void testLeadProcessorBatch(){

            

            // Load Test Data

            List<Lead> leads = new List<Lead>();

            for(Integer i=1;i<=200;i++){

                leads.add(new Lead(LastName='LeadLstName', Company='SalesForce'));

            }

            insert leads;

            // perfor test 

            Test.startTest();

            LeadProcessor lp = new LeadProcessor();

            Id batchId = Database.executeBatch(lp,200);

            Test.stopTest();

            

            //check result

            List<Lead> updatedLeads = [SELECT Id FROM Lead WHERE LeadSource='Dreamforce'];

            System.assertEquals(200, updatedLeads.size(),'ERROR: at least 1 Lead record not updated correctly');

        }

    }

     

    If you find your Solution then mark this as the best answer. 

     

    Thank you!

     

    Regards,

     

    Suraj Tripathi
0/9000