+ Start a Discussion
santhosh konathala 17santhosh konathala 17 

Hi I am new to Test classes can anybody suggest how to write a Test class for the below Trigger?

trigger Deleteres on Restintegration__c(after update) {
 List<Id> lstId = new List<Id>();

if(trigger.isupdate)
{
for(Restintegration__c res : trigger.new)
{
if(res.id != null)
{
lstId.add(res.id) ;
}
}
 }

for(Restintegration__c res: Trigger.old){
        List<Restintegration__c > existoppList =  [Select Id from Restintegration__c where  id in = lstId];
        delete existoppList;
    }
 
}
Best Answer chosen by santhosh konathala 17
Deepak Pandey 13Deepak Pandey 13
@isTest
private class testres {
static testMethod void mytestres() {
        // Setup the Restintegration__c  record
        Restintegration__c  rese  = new Restintegration__c ();
           rese.name = 'Deep';
        insert rese ;
        rese.name = 'San';
        update rese;
    }
}


after created class click on "run test" your trigger coverage has been reduce 

All Answers

Deepak Pandey 13Deepak Pandey 13
@isTest
private class testres {
static testMethod void mytestres() {
        // Setup the Restintegration__c  record
        Restintegration__c  rese  = new Restintegration__c ();
           rese.name = 'Deep';
        insert rese ;
        rese.name = 'San';
        update rese;
    }
}


after created class click on "run test" your trigger coverage has been reduce 
This was selected as the best answer
vinita kumari 8vinita kumari 8
Hi,
Below code should be helpful for you.

@isTest
public class DeleteresTest{
     public static testMethod void test1(){
     
           Restintegration__c rst = new Restintegration__c();
           rst.Name = 'Test record1';    // write this line if Name field is not autonumber.
           // define value for all required fields as defined for Name field.
           insert rst;

           update rst;
     }
}
santhosh konathala 17santhosh konathala 17
Thanks a Lot now I Have achieved 100% code coverage...
 
Deepak Pandey 13Deepak Pandey 13
but trigger best way to write use trigger handler. Use Trailhead to improve your development skill.
santhosh konathala 17santhosh konathala 17
Hi Deepak,

have a small doubt ..

In above code we are not refering any code from Trigger in Test class then How it  covers the code coverage..
once I was executed my Trigger, then code coverage was reached to 100% ..plz explain me...How it possible...
Deepak Pandey 13Deepak Pandey 13
 you had write trigger on resrvation__c. if any trigger writing in reservation__c which is also counting in this test class. but matter the secend trigger have something extra action then the coverage will be not 75%.
santhosh konathala 17santhosh konathala 17
Hi Deepak,

Thanks a lot Now I got it...