You need to sign in to do that
Don't have an account?
Sachin Sharma 4089
Test class is covering 72% of code
Hi
i have a batch class and i'm wirting a test class for that batch class but the test class is covering only 72% of code. So please help me to write a test class which will cover 100% of batch class code. Thank you in advance.
this is my batch class:-
public class MyBatchable implements Database.Batchable<SObject> {
public Database.QueryLocator start(Database.BatchableContext context) {
return Database.getQueryLocator([select Id from Account where name like 'acc%' order by Name]);
}
public void execute(Database.BatchableContext context, List<Account> scope) {
try{
Account[] updates = new Account[] {};
for (AggregateResult ar : [select AccountId a, count(Id) c from Contact where AccountId in :scope and active__c=true group by AccountId])
{
updates.add(new Account(Id = (Id) ar.get('a'), No_of_Active_Contacts__c = (Decimal) ar.get('c')));
}
update updates;
}
catch(Exception e) {
System.debug('An exception occurred: ' + e.getMessage());
}
}
public void finish(Database.BatchableContext context) {
}
}
This is the test class i have written:-
@isTest
public class MyBatchableTest {
static testMethod void testme(){
Account l = new Account();
l.name = 'acc1';
insert l;
Test.startTest();
MyBatchable ba= new MyBatchable();
Id jobid= Database.executeBatch(ba,5);
Test.stopTest();
Account acc = [select id from account];
System.assertEquals(5,acc.no_of_active_contacts__c);
}
}
Thanks and regards
Sachin Sharma
i have a batch class and i'm wirting a test class for that batch class but the test class is covering only 72% of code. So please help me to write a test class which will cover 100% of batch class code. Thank you in advance.
this is my batch class:-
public class MyBatchable implements Database.Batchable<SObject> {
public Database.QueryLocator start(Database.BatchableContext context) {
return Database.getQueryLocator([select Id from Account where name like 'acc%' order by Name]);
}
public void execute(Database.BatchableContext context, List<Account> scope) {
try{
Account[] updates = new Account[] {};
for (AggregateResult ar : [select AccountId a, count(Id) c from Contact where AccountId in :scope and active__c=true group by AccountId])
{
updates.add(new Account(Id = (Id) ar.get('a'), No_of_Active_Contacts__c = (Decimal) ar.get('c')));
}
update updates;
}
catch(Exception e) {
System.debug('An exception occurred: ' + e.getMessage());
}
}
public void finish(Database.BatchableContext context) {
}
}
This is the test class i have written:-
@isTest
public class MyBatchableTest {
static testMethod void testme(){
Account l = new Account();
l.name = 'acc1';
insert l;
Test.startTest();
MyBatchable ba= new MyBatchable();
Id jobid= Database.executeBatch(ba,5);
Test.stopTest();
Account acc = [select id from account];
System.assertEquals(5,acc.no_of_active_contacts__c);
}
}
Thanks and regards
Sachin Sharma
Try the Below Code Please Mark It As Best answer if it Helps
Thank you!
All Answers
Try the Below Code Please Mark It As Best answer if it Helps
Thank you!
Thanks for your reply. This code is covering 90% of batch class code but it is not covering the catch block. If you resolve this it'll be great.
Thanks and regards
Sachin Sharma
I've Made Some Changes Just Copy Paste The Below Code Please Don't Forget To Mark It As Best answer if it Helps
Thank you!
Thank you for your reply. can you help me to write negative test cases in the following test class?
Account Acc = new Account();
Acc.Name='acc1';
insert Acc;
contact con= new contact();
con.LastName = 'test';
con.AccountId = Acc.Id;
con.active__c = true;
insert con;
test.startTest();
MyBatchable ba= new MyBatchable();
Id jobid= Database.executeBatch(ba,5);
test.stopTest();
}
Thanks and Regards
Sachin
Please Close Your Query By Marking It Best Answer If Helps As it Helps Others Also
Thank You!