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 

how to do negative testing in apex test class?

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
CharuDuttCharuDutt
Hii Sachin Sharma
Try The Below Code
@isTest
public class MyBatchableTest {
    static testMethod void testme(){
        Account Acc = new Account();                                             
        Acc.Name='Test';
        insert Acc;
        
        contact con= new contact();
        con.LastName = 'testCon';
        con.AccountId = Acc.Id;
        con.active__c = False;
        
        insert con;
        test.startTest();
        MyBatchable ba= new MyBatchable();
        Id jobid= Database.executeBatch(ba,5);
        test.stopTest();
    }
}
Please Don't Forget To Mark it As Best Answer if ti Helps
Thank You!

 
Sachin Sharma 4089Sachin Sharma 4089
Hi Charu

Thanks for your reply. I have already tried this code and after thar the code coverage is 72%.


Thanks and Regards
Sachin
CharuDuttCharuDutt
Hii Sachin 
What Do You Mean By Negative Test Class?
Sachin Sharma 4089Sachin Sharma 4089
Hi Charu
It means that if i enter invalid data in the test class then what will happen?
CharuDuttCharuDutt
Hi Sachin 
Below False Data
Try It
@isTest
public class MyBatchableTest {
    static testMethod void testme(){
        Account Acc = new Account();                                             
        Acc.Name='Test';
        insert Acc;
        
        contact con= new contact();
        con.LastName = 'testCon';
        con.AccountId = Acc.Id;
        con.active__c = False;
        
        insert con;
        test.startTest();
        MyBatchable ba= new MyBatchable();
        Id jobid= Database.executeBatch(ba,5);
        test.stopTest();
    }
}
Please Don't Forget To Like And  Mark it As Best Answer if ti Helps
Thank You!

 
jimmy parkerjimmy parker
It means that if we enter invalid data (https://harrystylesmerch.net/category/shirts/)in the test class then what will happen? I think destroy....!