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
FinneyFinney 

Help please with Apex Test Class for code coverage

Dear All,

I have created a trigger for Candidate which on update creates an Employee record.

I am unable to cover the code with the test class I have written.

I am posting both the trigger and the class. Please help me.

TRIGGER

trigger EmployeeCreate on SFDC_Candidate__c (before update) {
  integer i=1;
  for (SFDC_Candidate__c can : trigger.new) {
    SFDC_Employee__c emp = new SFDC_Employee__c();
    SFDC_Candidate__c beforeUpdate = System.Trigger.oldMap.get(can.Id);
    
      system.debug(beforeUpdate.Create_Employee__c+'after----'+can.Create_Employee__c);
     if (beforeUpdate.Create_Employee__c!= can.Create_Employee__c){
    if(can.Create_Employee__c == True){
  
    emp.Candidate__c = can.id;
    emp.OwnerId = '005A0000000dy1y';
    emp.Name = can.SFDC_Candidate_Name__c;
    emp.Email_Address__c = can.SFDC_Candidate_Email__c;
    emp.Emergency_Contact_Phone__c = can.Preferred_Phone__c;
    emp.DOB__c = can.SFDC_Candidate_Birthdate__c;
    emp.Personal_Mobile__c = can.Mobile__c;
    emp.Gender__c = can.Gender__c;
 
   
   
   
    insert emp;
    }
   }
  }
}


TEST CLASS

@isTest
private class UnitTestsEmployeeCreate {

    static testMethod void myUnitTest() {
        // TO DO: implement unit test
      
        User u = [select id from user where FirstName = 'Lorenz'];
        Datetime todays= datetime.now();
        SFDC_Candidate__c can = new SFDC_Candidate__c();
        can.OwnerId = u.Id;
        can.SFDC_Candidate_Name__c = 'Test Name';
        can.SFDC_Candidate_Email__c = 'candidate@cansk.com';
        can.Preferred_Phone__c = '4568786588';
        can.Mobile__c = '5454454';
        can.Create_Employee__c = true;
        can.Gender__c = 'Female';
       
       
        insert can;
       
        SFDC_Employee__c emp = new SFDC_Employee__c();
        if(can.Create_Employee__c == true){
        emp.Candidate__c = can.id;
        emp.Name = can.SFDC_Candidate_Name__c;
        emp.Email_Address__c = can.SFDC_Candidate_Email__c;
        emp.Emergency_Contact_Phone__c = can.Preferred_Phone__c;
        emp.Personal_Mobile__c = can.Mobile__c;
        emp.Gender__c = can.Gender__c;
        emp.Personal_Mobile__c = can.Mobile__c;
 
       }
       
        insert emp;
       
          }
}

Thanks,

Finney
Deepak Kumar ShyoranDeepak Kumar Shyoran
Use below in your test class

@isTest
private class UnitTestsEmployeeCreate {

    static testMethod void myUnitTest() {
        // TO DO: implement unit test
      
        User u = [select id from user where FirstName = 'Lorenz'];
        Datetime todays= datetime.now();
        SFDC_Candidate__c can = new SFDC_Candidate__c();
        can.OwnerId = u.Id;
        can.SFDC_Candidate_Name__c = 'Test Name';
        can.SFDC_Candidate_Email__c = 'candidate@cansk.com';
        can.Preferred_Phone__c = '4568786588';
        can.Mobile__c = '5454454';
        can.Create_Employee__c = false ;
        can.Gender__c = 'Female';
      
        insert can;
	
        SFDC_Employee__c emp = new SFDC_Employee__c();
        emp.Candidate__c = can.id;
        emp.Name = can.SFDC_Candidate_Name__c;
        emp.Email_Address__c = can.SFDC_Candidate_Email__c;
        emp.Emergency_Contact_Phone__c = can.Preferred_Phone__c;
        emp.Personal_Mobile__c = can.Mobile__c;
        emp.Gender__c = can.Gender__c;
        emp.Personal_Mobile__c = can.Mobile__c;
 
       
        insert emp;
		
       can.Create_Employee__c = true ;
	   update can ;
          }
}

James LoghryJames Loghry
A couple of things:  
  1. Your trigger doesn't support bulkification (handling of multiple records), and it is inserting records in a loop. You can fix this by moving adding your employee object in a list and performing the insert outside of the for loop.  See this presentation for more info on trigger best practices: https://www.youtube.com/watch?v=D7mqMYliy3A
  2. Additionally, your trigger only runs on "update", and you're only inserting the record, you're not updating the record.  What you'll need to do to fix your coverage issue is to originally set your "Can_Create_Employee__c" field to false, then insert the candidate record, then update the "Can_Create_Employee__c" field to true and update the record.  The update will fire your trigger.
  3. Additionally, you you need to add System.asserts to test your expected results.
I've fixed your original test case for you in a general sense.  You're still going to write additional test cases for boundary and other conditions, however.   Meaning, you need to test if the record is updated, but Create_Employee__c doesn't change for instance.


@isTest
private class UnitTestsEmployeeCreate {

    static testMethod void myUnitTest() {
        User u = [select id from user where FirstName = 'Lorenz'];
        Datetime todays= datetime.now();
        SFDC_Candidate__c can = new SFDC_Candidate__c();
        can.OwnerId = u.Id;
        can.SFDC_Candidate_Name__c = 'Test Name';
        can.SFDC_Candidate_Email__c = 'candidate@cansk.com';
        can.Preferred_Phone__c = '4568786588';
        can.Mobile__c = '5454454';
        //can.Create_Employee__c = true;
        can.Create_Employee__c = false;
        can.Gender__c = 'Female';
        insert can;
   
        //Update the record and force the trigger to run.
        can.Create_Employee__c = true;
        update can;

        System.assertEquals(1,[Select Id From SFDC_Employee__c].size());
    }
}