• Sachin Sharma 4089
  • NEWBIE
  • 20 Points
  • Member since 2021

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 3
    Questions
  • 6
    Replies
Hi

i have a batch class and i have written a test class for that batch class. it is working fine. But i don't know how to do negative testing for that batch class.

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) {
    }
}


Test class for batch 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
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
Hi, i have just started salesforce and i have a situation in which i want to insert 300 contacts for each account and i have 100 accounts in my salesforce org. i have already read some articles on batch apex but i'm unable to understand how to do it. Thanks in advance if anyone solve this for me.
Hi

i have a batch class and i have written a test class for that batch class. it is working fine. But i don't know how to do negative testing for that batch class.

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) {
    }
}


Test class for batch 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
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
Hi, i have just started salesforce and i have a situation in which i want to insert 300 contacts for each account and i have 100 accounts in my salesforce org. i have already read some articles on batch apex but i'm unable to understand how to do it. Thanks in advance if anyone solve this for me.