You need to sign in to do that
Don't have an account?
vijay T 37
I have one batch class Unable to write test class
This is Batch Class
global class BatchAccountDescUpdate implements Database.Batchable<sObject> {
global Database.QueryLocator start(Database.BatchableContext BC){
String query = 'SELECT description FROM Account';
return Database.getQueryLocator(query);
//Even we can write like below also
//return Database.QueryLocator('SELECT description FROM Account')
}
global void execute(Database.BatchableContext BC, List<Account> scope){
for(Account a: scope){
if(a.description == 'Not Available'){
a.Description = 'Please fill details, else record will be made inactive!';
}
Update scope;
}
}
global void finish(Database.BatchableContext BC){
//code for sending email
//calling another batch
//sending state of processing records ** using stateful
}
}
Below is my Test Class
@isTest
public class BatchAccountDescUpdateTest
{
static testMethod void testMethod1()
{
List<Account> lstAccount = new List<Account>();
for(Integer i=0 ;i <200;i++)
{
Account acc = new Account();
if(acc.description == 'Not Available'){
acc.description = 'Please fill details, else record will be made inactive!';
}
lstAccount.add(acc);
}
insert lstAccount;
Test.startTest();
BatchAccountDescUpdate obj = new BatchAccountDescUpdate();
DataBase.executeBatch(obj);
Test.stopTest();
}
}
global class BatchAccountDescUpdate implements Database.Batchable<sObject> {
global Database.QueryLocator start(Database.BatchableContext BC){
String query = 'SELECT description FROM Account';
return Database.getQueryLocator(query);
//Even we can write like below also
//return Database.QueryLocator('SELECT description FROM Account')
}
global void execute(Database.BatchableContext BC, List<Account> scope){
for(Account a: scope){
if(a.description == 'Not Available'){
a.Description = 'Please fill details, else record will be made inactive!';
}
Update scope;
}
}
global void finish(Database.BatchableContext BC){
//code for sending email
//calling another batch
//sending state of processing records ** using stateful
}
}
Below is my Test Class
@isTest
public class BatchAccountDescUpdateTest
{
static testMethod void testMethod1()
{
List<Account> lstAccount = new List<Account>();
for(Integer i=0 ;i <200;i++)
{
Account acc = new Account();
if(acc.description == 'Not Available'){
acc.description = 'Please fill details, else record will be made inactive!';
}
lstAccount.add(acc);
}
insert lstAccount;
Test.startTest();
BatchAccountDescUpdate obj = new BatchAccountDescUpdate();
DataBase.executeBatch(obj);
Test.stopTest();
}
}
Try this, The issue was in the creation of account, not sure why you have if check.
I have gone through your problem.
Here is the code you can use it.
@isTest
public class BatchAccountDescUpdate_Test
{
static testMethod void batchAccount_Test()
{
List<Account> accountList = new List<Account>();
Account acc = new Account();
acc.Name = 'Test';
acc.description = 'Not Available';
accountList.add(acc);
insert accountList;
Test.startTest();
BatchAccountDescUpdate batchObj = new BatchAccountDescUpdate();
DataBase.executeBatch(batchObj);
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