You need to sign in to do that
Don't have an account?
asd@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');
}
}
}
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
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.
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