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
tomspousetomspouse 

Test case for the following trigger.

Hi,

How do i write a test case for the following trigger to cover 100% code coverage?

The one i have covers only 10%, am not sure which part of the code is not covered.

 

 

trigger AddInterview on Candidate_Requirement__c (after insert, after update) {
   
      
     for (Candidate_Requirement__c newInterview: Trigger.New) {
         if (newInterview.Status__c == 'Interview Stage' && newInterview.Candidates__c !=null && newInterview.Requirement__c != null) {
             String candR = newInterview.Id;
             List<Interview__c> intv = [select id,Candidates_submitted__c from Interview__c where Candidates_submitted__c = :candR ];
             
             if(intv.size() == 0){
                 String reqId = newInterview.Requirement__c;
                 List <Requirement__c> reqList = [select id, Contact__c from Requirement__c where id = :reqId ];
                 String candId =newInterview.Candidates__c;
                 List <Candidates__c> candList = [select id, Name from Candidates__c where id = :candId ];
                 String contId = reqList[0].Contact__c;
                 List <Contact> contList = [select id, MAILINGSTREET,MAILINGCITY,MAILINGSTATE,MAILINGPOSTALCODE,MAILINGCOUNTRY from Contact where id = :contId ];
                 String mAddress= '';
                 if(!contList.isEmpty()){
                 
                     mAddress = contList[0].MAILINGSTREET +' '+ contList[0].MAILINGCITY+', '+ contList[0].MAILINGSTATE+' '+contList[0].MAILINGPOSTALCODE+' '+contList[0].MAILINGCOUNTRY;
                 }
                 if (!reqList.isEmpty() && !candList.isEmpty()){
                 Requirement__c req = reqList[0];
                 Candidates__c cand = candList[0];
                 Contact cont = contList[0];
                 

 

                 Interview__c interviews = new Interview__c(
                             Name = cand.Name, 
                             Candidates_submitted__c = newInterview.Id,
                             Client_Names__c = cont.Id,
                             Address_of_Interview__c = mAddress,
                             Candidate_Names__c = cand.Id,
                             Requirement_Names__c = req.Id);
                           
                           
                 insert interviews;
                 }
                 
             }
          }
      }
}

 

Test case:

 

@isTest
private class UpdateInterviewTest {
  
    static testMethod void testUpdateInterview() {
    
    
         //Insert contact record
         Contact con = new Contact(
                Lastname = 'test contact',
                MAILINGSTREET = 'street',
                MAILINGCITY = 'city',
                MAILINGSTATE = 'state',
                MAILINGPOSTALCODE = 'code',
                MAILINGCOUNTRY = 'country');
                
             insert con;
             con = [Select Id,Lastname, MAILINGSTREET,MAILINGCITY,MAILINGSTATE,MAILINGPOSTALCODE,MAILINGCOUNTRY from Contact where id = :con.Id];
            
            //Insert Requiremnt Record
            Requirement__c req = new  Requirement__c(
                Contact__c = con.Id,
                Name = 'test req');
                
            insert req;
            req = [Select Id, Name,Contact__c from Requirement__c where Id = :req.Id];
            
            //Insert Candidates record
            Candidates__c cand = new Candidates__c (
                Name = 'test name',
                Candidate_Source__c = 'test picklist');
            insert cand;
            cand = [Select Id, Name,Candidate_Source__c from Candidates__c where Id = :cand.Id]; 
          
           //Insert Candidate submitted to requiremnt record whose status is not Interview Stage
           Candidate_Requirement__c cr = new Candidate_Requirement__c(
                Status__c = 'Offer Made',
                Candidates__c = cand.Id,
                Requirement__c = req.Id);
            
            insert cr;
            cr = [Select Id,Status__c,Candidates__c,Requirement__c from Candidate_Requirement__c where Id = :cr.Id];
            
          
                   
          Interview__c[] intv = [select Id ,Candidates_submitted__c from Interview__c where  Candidates_submitted__c= :cr.Id];
          if(intv.size() > 0)
          System.assertEquals(intv[0].Candidates_submitted__c, cr.Id); 
          
          }
         
     
   }

 

@isTest
private class UpdateInterviewTest {
 
    static testMethod void testUpdateInterview() {
    
    
         //Insert contact record
         Contact con = new Contact(
                Lastname = 'test contact',
                MAILINGSTREET = 'street',
                MAILINGCITY = 'city',
                MAILINGSTATE = 'state',
                MAILINGPOSTALCODE = 'code',
                MAILINGCOUNTRY = 'country');
                
             insert con;
             con = [Select Id,Lastname, MAILINGSTREET,MAILINGCITY,MAILINGSTATE,MAILINGPOSTALCODE,MAILINGCOUNTRY from Contact where id = :con.Id];
            
            //Insert Requiremnt Record
            Requirement__c req = new  Requirement__c(
                Contact__c = con.Id,
                Name = 'test req');
                
            insert req;
            req = [Select Id, Name,Contact__c from Requirement__c where Id = :req.Id];
            
            //Insert Candidates record
            Candidates__c cand = new Candidates__c (
                Name = 'test name',
                Candidate_Source__c = 'test picklist');
            insert cand;
            cand = [Select Id, Name,Candidate_Source__c from Candidates__c where Id = :cand.Id];
          
           //Insert Candidate submitted to requiremnt record whose status is not Interview Stage
           Candidate_Requirement__c cr = new Candidate_Requirement__c(
                Status__c = 'Offer Made',
                Candidates__c = cand.Id,
                Requirement__c = req.Id);
            
            insert cr;
            cr = [Select Id,Status__c,Candidates__c,Requirement__c from Candidate_Requirement__c where Id = :cr.Id];
            
          
                   
          Interview__c[] intv = [select Id ,Candidates_submitted__c from Interview__c where  Candidates_submitted__c= :cr.Id];
          if(intv.size() > 0)
          System.assertEquals(intv[0].Candidates_submitted__c, cr.Id);
          
          }
         
     
   }
Best Answer chosen by Admin (Salesforce Developers) 
sfdcfoxsfdcfox

Before you worry about your test code, worry about the fact that your code doesn't handle "bulk" processing. Try this link here:

 

http://wiki.developerforce.com/index.php/Best_Practice:_Bulkify_Your_Code

 

In particular, note that you have queries inside a loop, which is definitely a poor practice in coding.

 

However, as far as testing is concerned, you need more than one test. Each test in a test class needs to test one particular branch of the trigger. For example, you would test the following conditions:

 

* If intv size is zero.

* If intv size is not zero.

* If contList is empty.

* If contList is not empty.

* If reqList is empty.

* If reqList is not empty.

* If candList is empty.

* If candList is not empty.

 

You need to test not just a single permutation, but you should write one test for each particular scenario.

 

The alternative would be to insert a bunch of records, each record producing a particular scenario, with the sum effect of covering 100% of the trigger code.

 

Your code appears to be 100% coverable, but it's going to take a decent sized test to get there.

 

Again, I should stress that you should first bulkify your code before you even try to shoe-horn this into production, because if you don't, you'll probably forget to do it later, and some administrator months down the road will be trying to import a file, and it won't work because your trigger keeps choking on a list of more than 33 Candidate_Requirement__c records.