You need to sign in to do that
Don't have an account?
suji srinivasan
how to get 100% coverage for batch apex
Batch
global class UpdateAccountRating implements Database.Batchable<sObject>, Database.Stateful {
global Integer recordsProcessed = 0;
global Database.QueryLocator start(Database.BatchableContext bc) {
string query='select id, stageName,AccountId, Account.Rating from opportunity where stageName IN (\'workInprogress,Delivered,closedwon\') AND AccountId !=Null';
return Database.getQueryLocator(query);
}
global Void execute(Database.BatchableContext bc, List<opportunity> Scope){
for(opportunity o :Scope){
o.Account.Rating='client';
system.debug('Rating'+o.Account.Rating);
recordsProcessed = recordsProcessed + 1;
}
update Scope;}
global void finish(Database.BatchableContext bc){
System.debug(recordsProcessed + 'records processed. Shazam!');
AsyncApexJob job = [SELECT Id, Status,NumberOfErrors,JobItemsProcessed,TotalJobItems, CreatedBy.Email
FROM AsyncApexJob
WHERE Id = :bc.getJobId()];
}
}
test class
@isTest
public class UpdateAccountRatingTest {
@isTest
public static void unit_test(){
//insert All mandatory fields
Account acc =new Account();
acc.Name = 'test';
acc.Rating='prospect';
insert acc;
//insert All mandatory fields
Opportunity opp = new opportunity();
opp.Name = 'test opp';
opp.Account.Rating='prospect';
opp.Accountid = acc.Id;
opp.stageName = 'workInprogress';
opp.CloseDate = system.today()+5;
insert opp;
Test.startTest();
UpdateAccountRating uar = new UpdateAccountRating();
Database.executeBatch(uar);
String sch = '0 0 0 1 * ? *';
string JobID = system.schedule('BatchJob',sch, new UpdateAccountRatingSchedular());
Test.stopTest();
}
}
thanks in advance
global class UpdateAccountRating implements Database.Batchable<sObject>, Database.Stateful {
global Integer recordsProcessed = 0;
global Database.QueryLocator start(Database.BatchableContext bc) {
string query='select id, stageName,AccountId, Account.Rating from opportunity where stageName IN (\'workInprogress,Delivered,closedwon\') AND AccountId !=Null';
return Database.getQueryLocator(query);
}
global Void execute(Database.BatchableContext bc, List<opportunity> Scope){
for(opportunity o :Scope){
o.Account.Rating='client';
system.debug('Rating'+o.Account.Rating);
recordsProcessed = recordsProcessed + 1;
}
update Scope;}
global void finish(Database.BatchableContext bc){
System.debug(recordsProcessed + 'records processed. Shazam!');
AsyncApexJob job = [SELECT Id, Status,NumberOfErrors,JobItemsProcessed,TotalJobItems, CreatedBy.Email
FROM AsyncApexJob
WHERE Id = :bc.getJobId()];
}
}
test class
@isTest
public class UpdateAccountRatingTest {
@isTest
public static void unit_test(){
//insert All mandatory fields
Account acc =new Account();
acc.Name = 'test';
acc.Rating='prospect';
insert acc;
//insert All mandatory fields
Opportunity opp = new opportunity();
opp.Name = 'test opp';
opp.Account.Rating='prospect';
opp.Accountid = acc.Id;
opp.stageName = 'workInprogress';
opp.CloseDate = system.today()+5;
insert opp;
Test.startTest();
UpdateAccountRating uar = new UpdateAccountRating();
Database.executeBatch(uar);
String sch = '0 0 0 1 * ? *';
string JobID = system.schedule('BatchJob',sch, new UpdateAccountRatingSchedular());
Test.stopTest();
}
}
thanks in advance
your query was in correct in the bact apex that is the reason its not covering.
Your query considering 'workInprogress,Delivered,Closed won' as single word.
try with below belo class and test class you will get 100% .
Batch class:
Test Class:
If this helps, Please mark it as best answer.
thanks!!
All Answers
Is your batch class working as expected??
Thanks!!
yeah
your query was in correct in the bact apex that is the reason its not covering.
Your query considering 'workInprogress,Delivered,Closed won' as single word.
try with below belo class and test class you will get 100% .
Batch class:
Test Class:
If this helps, Please mark it as best answer.
thanks!!