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
AmberTaylorAmberTaylor 

Need Help writting a 100% code coverage test class for Batch

Hi there, 

I'm quite stuck here. 
I've written a test class for my batch but it doesn't appears to be running. It stays at 0% :( 

I'm missing something but don't know what. 

Can someone help me figure it out? 

Here is my batch class : 
global class BatchUpdateAllLeads implements Database.Batchable <SObject> {
//START METHOD
    global Database.QueryLocator start(Database.BatchableContext bc){
        String Query='Select id,TECH_Update__c from Lead where TECH_Update__c=false AND Converted=false ' ;
        return Database.getQueryLocator(Query);
            }
//EXECUTE METHOD
    global void execute(Database.BatchableContext bc, List<Lead> scope){
        for(Lead l: scope){
            l.TECH_Update__c = true;
        }
        update scope;
    }
//FINISH METHOD
    global void finish(Database.BatchableContext bc){
        Id job= bc.getJobId();
        System.debug(job);
    }
}

And this is my attemp of writting a proper test class :( 
@istest
private class BatchUpdateAllLeads_TEST {
    @istest
    static void testAcc(){
        List<Lead> l = new List<Lead>();
        Lead l1 = new Lead(Company	='BTP',LastName = 'Test',SIRET__c='12659991955626',Numero_d_ordre__c='9999-99-99',Region_GRDF__c='Ile de France',Tech_Update__c=false,IsConverted=false);
        l.add(l1);

        Lead l2= new Lead(Company	='Comp',LastName = 'Test2',SIRET__c='12659991955687',Numero_d_ordre__c='1111-99-99',Region_GRDF__c='Ile de France',Tech_Update__c=false, IsConverted=true);
        l.add(l2);

        Lead l3= new Lead(Company	='Agny', LastName = 'Test3', SIRET__c='19959991988687', Numero_d_ordre__c='1111-99-11', Region_GRDF__c='Ile de France', Tech_Update__c=true,IsConverted=true);
        l.add(l3);
        insert l;
   
    Test.startTest();
    BatchUpdateAllLeads ap= new BatchUpdateAllLeads();
    Id jobid= Database.executeBatch(ap);
    Test.stopTest();
    }
}

Ty very much for your help
Best Answer chosen by AmberTaylor
sachinarorasfsachinarorasf
Hi AmberTaylor ,

Please update your both class Apex Class as well as test class by the following code to achieve 100% Code Coverage.

Apex Class.
global class BatchUpdateAllLeads implements Database.Batchable <SObject> {
//START METHOD
    global Database.QueryLocator start(Database.BatchableContext bc){
        String Query='Select id,TECH_Update__c from Lead where TECH_Update__c=false AND isConverted=false' ;
        return Database.getQueryLocator(Query);
            }
//EXECUTE METHOD
    global void execute(Database.BatchableContext bc, List<Lead> scope){
        for(Lead l: scope){
            l.TECH_Update__c = true;
        }
        update scope;
    }
//FINISH METHOD
    global void finish(Database.BatchableContext bc){
        Id job= bc.getJobId();
        System.debug(job);
    }
}

Test Class.
@istest
private class BatchUpdateAllLeads_TEST {
    @istest
    static void testAcc(){
       List<Lead> l = new List<Lead>();
        Lead l1 = new Lead(Company    ='BTP',LastName = 'Test',Tech_Update__c=false,IsConverted=false);
        l.add(l1);

        insert l;
  
    Test.startTest();
    BatchUpdateAllLeads ap= new BatchUpdateAllLeads();
    Id jobid= Database.executeBatch(ap);
    Test.stopTest();
    }
}



I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Sachin Arora
www.sachinsf.com

All Answers

SarvaniSarvani
Hi Amber,

Please try chaging you batch class query on line 4. You field for converted should be isConverted like below:
String Query='Select id,TECH_Update__c from Lead where TECH_Update__c=false AND isConverted=false' ;

And try with below test class:
@istest
private class BatchUpdateAllLeads_TEST {
    @istest
    static void testAcc(){
        
   Lead l1 = new Lead(Company='BTP',LastName = 'Test',SIRET__c='12659991955626',Numero_d_ordre__c='9999-99-99',Region_GRDF__c='Ile de France',Tech_Update__c=false);
       insert l1;

   Test.startTest();
    BatchUpdateAllLeads ap= new BatchUpdateAllLeads();
    Id jobid= Database.executeBatch(ap);
    Test.stopTest();
    }
}

Hope this helps!

Thanks
sachinarorasfsachinarorasf
Hi AmberTaylor ,

Please update your both class Apex Class as well as test class by the following code to achieve 100% Code Coverage.

Apex Class.
global class BatchUpdateAllLeads implements Database.Batchable <SObject> {
//START METHOD
    global Database.QueryLocator start(Database.BatchableContext bc){
        String Query='Select id,TECH_Update__c from Lead where TECH_Update__c=false AND isConverted=false' ;
        return Database.getQueryLocator(Query);
            }
//EXECUTE METHOD
    global void execute(Database.BatchableContext bc, List<Lead> scope){
        for(Lead l: scope){
            l.TECH_Update__c = true;
        }
        update scope;
    }
//FINISH METHOD
    global void finish(Database.BatchableContext bc){
        Id job= bc.getJobId();
        System.debug(job);
    }
}

Test Class.
@istest
private class BatchUpdateAllLeads_TEST {
    @istest
    static void testAcc(){
       List<Lead> l = new List<Lead>();
        Lead l1 = new Lead(Company    ='BTP',LastName = 'Test',Tech_Update__c=false,IsConverted=false);
        l.add(l1);

        insert l;
  
    Test.startTest();
    BatchUpdateAllLeads ap= new BatchUpdateAllLeads();
    Id jobid= Database.executeBatch(ap);
    Test.stopTest();
    }
}



I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Sachin Arora
www.sachinsf.com
This was selected as the best answer
AmberTaylorAmberTaylor
Hello both, 

You helped me a lot to correct my careless mistakes. Really when i posted it, seemed all fine. 

Ty very much for this second look and your precious time. 

Now i Hit my 100% code coverage ! 
Suresh hansaSuresh hansa
 Top 1 Website For Quotes for reading
Attitude Status: https://www.hindiattitudestatus.in/2020/03/attitude-status-in-hindi-for-whatsapp.html
Website: https://www.hindiattitudestatus.in
Top 2 Website for Sarkari Bank Jobs (https://www.sarkaribankjobs.com)
Ipbs po, Sbi Jobs,  IBPS PO, IBPS Clerk, SBI Clerk visit: https://www.sarkaribankjobs.com
 
Srikanth Kolanu 18Srikanth Kolanu 18
check this link for examples on batch apex and test classes for it
http://salesforcecodes.com/test-class-for-batch-apex/
Manoj ojhaManoj ojha
If you are having problems then you should visit this (http://salesforcecodes.com/test-class-for-batch-apex/) website once. I also have a question with you that I have created a website above stock marketing (https://www.connectmoney.in/calculator/zerodha-margin-calculator/121/" target="_blank) but I have to move from HTTP to HTTPS, If you have a code, Please tell
Spot NewsSpot News
Spot News 18 (https://www.spotnews18.com/) Is An Online News Website. At Spot News 18 You Find All The Latest News Updates Happening Around The World. Get Breaking News Online.