You need to sign in to do that
Don't have an account?
Ashu 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();
}
}
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();
}
}
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
Try the below code in your test class:
Thanks
Hope this helps.