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
asd@as.asdasd@as.asd 

Test for addError

how to cover addError in test class

 

 

trigger insertStudent on Student__c (before insert ) {
Map<id,class__C> claMap=new Map<id,class__C>([select NumberOfStudents__c,MaxSize__c from class__c]);
for(Student__c s:Trigger.new){
class__c cls=claMap.get(s.class__c);
if(cls.NumberOfStudents__c >=cls.MaxSize__c){
s.addError('Can\'t add');
}
}

}

Best Answer chosen by Admin (Salesforce Developers) 
asd@as.asdasd@as.asd

thanks for reply.

how can i check the error massage using system.asserts method.

 

Trigger

trigger insertStudent on Student__c (before insert ) {
Map<id,class__C> claMap=new Map<id,class__C>([select NumberOfStudents__c,MaxSize__c from class__c]);
for(Student__c s:Trigger.new){
class__c cls=claMap.get(s.class__c);
if(cls.NumberOfStudents__c >=cls.MaxSize__c){
s.addError('Can not add');
}
}
}

 

Test

@isTest
private class test_triggerOnMyCount{
static testmethod void triggerTest(){
class__C cls=new class__C();
cls.Name__c = 'Apex' ;
cls.MaxSize__c=1;
insert cls;
System.assertEquals(null,cls.NumberOfStudents__c );
Student__C std = new Student__C();
std.Last_Name__c='Tejpal';
std.class__C=cls.id;
insert std;
cls=[select MyCount__c from class__C where id=: cls.id];
System.assertEquals(1,cls.NumberOfStudents__c );




Student__C newstd = new Student__C();
newstd .Last_Name__c='Tejpal';
newstd .class__C=cls.id;

insert newstd ;

}
catch (Exception e){

System.assert(e.getMessage().contains('Can not add'));


}


}

}


here my test class cover 100% code

All Answers

Chamil MadusankaChamil Madusanka

You need to insert value for NumberOfStudents__c which is greater than MaxSize__c. then run the test.

 

If a reply to a post answers your question or resolves your problem, please mark it as the solution to the post so that others may benefit.

asd@as.asdasd@as.asd

thanks for reply.

how can i check the error massage using system.asserts method.

 

Trigger

trigger insertStudent on Student__c (before insert ) {
Map<id,class__C> claMap=new Map<id,class__C>([select NumberOfStudents__c,MaxSize__c from class__c]);
for(Student__c s:Trigger.new){
class__c cls=claMap.get(s.class__c);
if(cls.NumberOfStudents__c >=cls.MaxSize__c){
s.addError('Can not add');
}
}
}

 

Test

@isTest
private class test_triggerOnMyCount{
static testmethod void triggerTest(){
class__C cls=new class__C();
cls.Name__c = 'Apex' ;
cls.MaxSize__c=1;
insert cls;
System.assertEquals(null,cls.NumberOfStudents__c );
Student__C std = new Student__C();
std.Last_Name__c='Tejpal';
std.class__C=cls.id;
insert std;
cls=[select MyCount__c from class__C where id=: cls.id];
System.assertEquals(1,cls.NumberOfStudents__c );




Student__C newstd = new Student__C();
newstd .Last_Name__c='Tejpal';
newstd .class__C=cls.id;

insert newstd ;

}
catch (Exception e){

System.assert(e.getMessage().contains('Can not add'));


}


}

}


here my test class cover 100% code

This was selected as the best answer