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); 
          
          }
         
     
   }
Best Answer chosen by Admin (Salesforce Developers) 
b-Forceb-Force

Please try with below code snippet

 

 

@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;
             
            //Insert Requiremnt Record
            Requirement__c req = new  Requirement__c
	    (
                Contact__c = con.Id,
                Name = 'test req');
            insert req;
           
            //Insert Candidates record
            Candidates__c cand = new Candidates__c (
                Name = 'test name',
                Candidate_Source__c = 'test picklist');
            insert cand;
           //Insert Candidate submitted to requiremnt record whose status is not Interview Stage
	   List<Candidate_Requirement__c> lstC=New List<Candidate_Requirement__c>();
	   //offer Made
           Candidate_Requirement__c cr = new Candidate_Requirement__c(
                Status__c = 'Offer Made',
                Candidates__c = cand.Id,
                Requirement__c = req.Id);
            lstC.add(cr);
	//'Interview Stage' req	
           Candidate_Requirement__c cr2 = new Candidate_Requirement__c(
                Status__c = 'Interview Stage',
                Candidates__c = cand.Id,
                Requirement__c = req.Id);
		lstC.add(cr2);
		insert lstC;	            
          
          //Updating list
	  lstC[0].Status__c='Interview Stage';
	  lstC[0].Status__c='Offer Made';
	  
	  update lstC;
	  }
         
     
   }

 

you can check , which lines of code are not executing, if you testing using salesforce UI or eclipse

 

Hope this will help you,

 

Thanks,

Bala

 

 

 

All Answers

b-Forceb-Force

Please try with below code snippet

 

 

@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;
             
            //Insert Requiremnt Record
            Requirement__c req = new  Requirement__c
	    (
                Contact__c = con.Id,
                Name = 'test req');
            insert req;
           
            //Insert Candidates record
            Candidates__c cand = new Candidates__c (
                Name = 'test name',
                Candidate_Source__c = 'test picklist');
            insert cand;
           //Insert Candidate submitted to requiremnt record whose status is not Interview Stage
	   List<Candidate_Requirement__c> lstC=New List<Candidate_Requirement__c>();
	   //offer Made
           Candidate_Requirement__c cr = new Candidate_Requirement__c(
                Status__c = 'Offer Made',
                Candidates__c = cand.Id,
                Requirement__c = req.Id);
            lstC.add(cr);
	//'Interview Stage' req	
           Candidate_Requirement__c cr2 = new Candidate_Requirement__c(
                Status__c = 'Interview Stage',
                Candidates__c = cand.Id,
                Requirement__c = req.Id);
		lstC.add(cr2);
		insert lstC;	            
          
          //Updating list
	  lstC[0].Status__c='Interview Stage';
	  lstC[0].Status__c='Offer Made';
	  
	  update lstC;
	  }
         
     
   }

 

you can check , which lines of code are not executing, if you testing using salesforce UI or eclipse

 

Hope this will help you,

 

Thanks,

Bala

 

 

 

This was selected as the best answer
tomspousetomspouse

@Bala,

Thanks alot, that did the trick!

b-Forceb-Force

Cool, it works for you ....

Happy clouding .....

 

Thanks,

Bala