You need to sign in to do that
Don't have an account?
Sachin 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
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
Try The Below Code Please Don't Forget To Mark it As Best Answer if ti Helps
Thank You!
Thanks for your reply. I have already tried this code and after thar the code coverage is 72%.
Thanks and Regards
Sachin
What Do You Mean By Negative Test Class?
It means that if i enter invalid data in the test class then what will happen?
Below False Data
Try It Please Don't Forget To Like And Mark it As Best Answer if ti Helps
Thank You!