function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Sachin Sharma 4089Sachin 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
Best Answer chosen by Sachin Sharma 4089
CharuDuttCharuDutt
Hii Sachin Sharma
Try the Below Code
@isTest
public class MyBatchableTest {
    static testMethod void testme(){
        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();
    }
}
Please Mark It As Best answer if it Helps
Thank you!

 

All Answers

CharuDuttCharuDutt
Hii Sachin Sharma
Try the Below Code
@isTest
public class MyBatchableTest {
    static testMethod void testme(){
        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();
    }
}
Please Mark It As Best answer if it Helps
Thank you!

 
This was selected as the best answer
Sachin Sharma 4089Sachin Sharma 4089
Hi CharuDutt

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
 
CharuDuttCharuDutt
Hii Sachin Sharma
I've Made Some Changes Just Copy Paste The Below Code
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 group by AccountId])
        {
            updates.add(new Account(Id = (Id) ar.get('a'), total_edit__c = (Decimal) ar.get('c')));
        }
            system.debug('updates==> '+updates);
        update updates;
           
         if(Test.isRunningTest())
			{
				throw new dmlException();
			}    
    }
        
        catch(dmlException e) {
            system.debug('updates==> ecep');
        System.debug('An exception occurred: ' + e.getMessage());
}
    }

    public void finish(Database.BatchableContext context) {
    }
}
Please Don't Forget To Mark It As Best answer if it Helps
Thank you!

 
 
Sachin Sharma 4089Sachin Sharma 4089
Hi Charu

Thank you for your reply. can you help me to write negative test cases in the following test class?
@isTest public class MyBatchableTest {
static testMethod void testme(){
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
CharuDuttCharuDutt
Hi Sachin

Please Close Your Query By Marking It Best Answer If Helps As it Helps Others Also
Thank You!