+ Start a Discussion
asd@as.asdasd@as.asd 

Test for Undelete

hello,

  i want to write a test class for a trigger but my test class cant cover Trigger on  undelete.

 Trigger 

trigger triggerOnMyCount on Student__c ( after insert , after delete ,after undelete) {

Map<Id,Class__c> claMap= new Map<id,Class__C>([Select c.NumberOfStudents__c,c.MyCount__c From Class__c c ]);
if(Trigger.isInsert){
for( student__c std : trigger.new ){
class__c cls=claMap.get(std.class__c);
cls.MyCount__c=cls.NumberOfStudents__c+1;
update cls;
}

}

if(trigger.isdelete){
for(student__c std : trigger.old){
class__c cls=claMap.get(std.class__c);
cls.MyCount__c=cls.NumberOfStudents__c-1;
update cls;
}
}

if(Trigger.isUnDelete){
for( student__c std : trigger.new ){
class__c cls=claMap.get(std.class__c);
cls.MyCount__c=cls.MyCount__c+1;
update cls;
}
}
}

 

TEST CLASS

@isTest
private class test_triggerOnMyCount{
static testmethod void triggerTest(){
class__C cls=new class__C();
cls.Name__c = 'Apex' ;
insert cls;
System.assertEquals(null,cls.MyCount__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.MyCount__c);
delete std;
cls=[select MyCount__c from class__C where id=: cls.id];
System.assertEquals(0,cls.MyCount__c);


}

}


please help

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

i solved my problem.

its a silly  problem.

TEST class

@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.MyCount__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.MyCount__c);
delete std;
cls=[select MyCount__c from class__C where id=: cls.id];
System.assertEquals(0,cls.MyCount__c);
undelete std;


cls=[select MyCount__c from class__C where id=: cls.id];
System.assertEquals(1,cls.MyCount__c);


}

}


All Answers

asd@as.asdasd@as.asd

i solved my problem.

its a silly  problem.

TEST class

@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.MyCount__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.MyCount__c);
delete std;
cls=[select MyCount__c from class__C where id=: cls.id];
System.assertEquals(0,cls.MyCount__c);
undelete std;


cls=[select MyCount__c from class__C where id=: cls.id];
System.assertEquals(1,cls.MyCount__c);


}

}


This was selected as the best answer
Navatar_DbSupNavatar_DbSup

Hi,

        Tryout this sample test code to get your requirement, it may help you.

 

@isTest
private class test_triggerOnMyCount{
static testmethod void triggerTest(){
class__C cls=new class__C();
cls.Name__c = 'Apex' ;
insert cls;
System.assertEquals(null,cls.MyCount__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.MyCount__c);
delete std;
cls=[select MyCount__c from class__C where id=: cls.id];
System.assertEquals(0,cls.MyCount__c);
undelete std;