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
Ashu sharma 38Ashu sharma 38 

test class for batch apex is not working

Hello,

As i am trying to achieve the code coverage but am not getting in batch apex,i have been seen the example share on the community,but please look into the below code:
global class batchApexForLocationBase implements database.Batchable <Sobject>{
    
    global database.QueryLocator start(database.BatchableContext bc){
        String s = '@@@@@****';
        return database.getQueryLocator('select id,name,Location__c,owner.name from lead where owner.name=:s');
    }
    global void execute (database.BatchableContext bc,list<lead> scpe){
        
        user u=[select id,name,alias from user where alias='nshar'];
        list<task> myTask=new list<Task>();
        for(lead l:scpe){
            
            if(l.location__C==null || l.Location__c==''){
                l.ownerId=u.id;
                task ts=new task();
                ts.Status='Not Started';
                ts.Subject='Checking for OwnerChange';
                ts.Priority='Normal';
                ts.whoId=l.Id;
                myTask.add(ts);
            }
        }
        update scpe;
        insert myTask;
    }
    global void finish(database.BatchableContext bc ){
    }
    
}
test class
========
@istest
public class testBatchApexForLocationBase {
    @istest
    static void myTestMethod(){
        list<lead> myLead=new list<lead>();
        for(integer i=0;i<200;i++){
            lead l=new lead(lastName='testingclass' +i,company='testing');
            myLead.add(l);
            
        }
        insert myLead;
        system.test.startTest();
        batchApexForLocationBase nw=new batchApexForLocationBase();
        database.executeBatch(nw);
        system.test.stopTest();
        
    }
    
    
}
Sampath SuranjiSampath Suranji
Hi,

try the 'return database.getQueryLocator('select id,name,Location__c,owner.name from lead where owner.name=:s');
code part without the where condition. According to your where condition there should not have any records in the context.

regards
Bhargavi TunuguntlaBhargavi Tunuguntla
Hi Nitish,

Try the below code in your test class:
 
@istest
public class testBatchApexForLocationBase {
    @istest
    static void myTestMethod(){
	
	Profile p = [SELECT Id FROM Profile WHERE Name='Standard User']; 
	User u = new User(Alias = 'standt', Email='standarduser@testorg.com', 
            EmailEncodingKey='UTF-8', Name='nshar', LanguageLocaleKey='en_US', 
            LocaleSidKey='en_US', ProfileId = p.Id, 
            TimeZoneSidKey='America/Los_Angeles', UserName='standarduser@testorg.com');
			insert u;
        User u1 = new User(Alias = 'standt', Email='standarduser@testorg.com', 
            EmailEncodingKey='UTF-8', Name='@@@@@****', LanguageLocaleKey='en_US', 
            LocaleSidKey='en_US', ProfileId = p.Id, 
            TimeZoneSidKey='America/Los_Angeles', UserName='standarduser@testorg.com');
			insert u1;

        System.runAs(u) {
        list<lead> myLead=new list<lead>();
        for(integer i=0;i<200;i++){
            lead l=new lead(lastName='testingclass' +i,company='testing');
            myLead.add(l);
            
        }
        insert myLead;
        system.test.startTest();
        batchApexForLocationBase nw=new batchApexForLocationBase();
        database.executeBatch(nw);
        system.test.stopTest();
		}
        
    }
    
    
}

Thanks
Hope this helps.​​​​​​​