Please follow the below points which helps you to resolve the issue:
Batch class:
Should write code in the batch class which handles both scenario(test code and normal execution) . Please see the code below for refrence.
Global Database.QueryLocator start(Database.BatchableContext BC){
String query='';
if(test.isrunningtest()){
query = 'Select id From User LIMIT 1';
}else{
query = 'Select id From User ';
}
return Database.getQueryLocator(query);
}
Test Class:
1. Create the dummy data which will be used in the batch class. 2. Create a instance of the batch class.
Please see the below code:
Id p = [select id from profile where name='System Administrator'].id;
Account ac = new Account(name ='Grazitti') ;
insert ac;
//Non-egencia select user
Contact con = new Contact(LastName ='testCon',AccountId = ac.Id);
insert con;
User user = new User(alias = 'test123', email='test123@noemail.com',
emailencodingkey='UTF-8', lastname='Testing', languagelocalekey='en_US',
localesidkey='en_US', profileid = p, country='United States',IsActive =true,
ContactId = con.Id, timezonesidkey='America/Los_Angeles', username='tester@noemail.com');
insert user;
[batchClassName] b1 = new [BatchClassName]();
ID batchprocessid1 = Database.executeBatch(b1,10);
Please mark as best answer if it solves your issue.
If yes then what is the problem using this :
Hope this helps!
Please follow the below points which helps you to resolve the issue:
Batch class:
Should write code in the batch class which handles both scenario(test code and normal execution) . Please see the code below for refrence.
Test Class:
1. Create the dummy data which will be used in the batch class.
2. Create a instance of the batch class.
Please see the below code:
Please mark as best answer if it solves your issue.
Regards,
Grazitti Team,
www.grazitti.com
Database.BatchableContext dbc;
BAClass batch_obj = new BAClass();
batch_obj.finish(dbc);
ID batchprocessid = Database.executeBatch(batch_obj);
test.stopTest();
getting error : Method does not exist or incorrect signature: test.startTest()
without writing test.startTest() and test.stopTest() methods............
Here the final method is calling two times but while calling executebatch the start & execute methods are not executing......
public class BAClass implements Database.Batchable<name__c>{
public Iterable<name__c> start(Database.BatchableContext bc) {
System.debug('START METHOD');
list<name__c> name_list = new list<name__c>();
name_list = [select id,name,qualification__c, student_id__c, email__c from name__c];
return name_list;
}
public void execute( Database.BatchableContext bc, list<name__c> l) {
System.debug('EXECUTE METHOD');
decimal i =1000;
for ( name__c n : l){
n.student_id__c = i;
i+=1;
}
update l;
}
public void finish(Database.BatchableContext bc) {
System.debug('Batch Apex program successfully executed..');
}
}
@isTest
public class BAClass_text {
static public testmethod void Batch() {
//test.startTest();
list<name__c> l = new list<name__c>();
Database.BatchableContext dbc;
BAClass batch_obj = new BAClass();
batch_obj.start(dbc);
batch_obj.execute(dbc, l);
batch_obj.finish(dbc);
//ID batchprocessid = Database.executeBatch(batch_obj);
//test.stopTest();
}
}