+ Start a Discussion
sumit dsumit d 

Test class coverage for trigger helper

Hi All,
         I have written a test class for trigger helper. Can anyone help me with the test coverage as it's not covering 75 %?
 My helper class is given below:-

// Helper class for Trigger_Registration
public without sharing class RegistrationTriggerHelper {

    public static List<rie__Registration__c> newRegistration = new List<rie__Registration__c>();
    public static List<rie__Registration__c> oldRegistration = new List<rie__Registration__c>();    
    public static Map<Id, rie__Registration__c> newMapRegistration = new Map<Id, rie__Registration__c>();
    public static Map<Id, rie__Registration__c> oldMapRegistration = new Map<Id, rie__Registration__c>(); 
    
    public static boolean runTrigger = TRUE;
    
    // method to link the contact and registration
    public static void linkContact(){
    
        List<rie__Registration__c> applicableRegistrations = new List<rie__Registration__c>();
        Set<String> emails = new Set<String>();
        for( rie__Registration__c regObj : newRegistration ) {
            if( regObj.rie__Email__c != Null ) {
               emails.add( regObj.rie__Email__c ); 
               applicableRegistrations.add( regObj ); 
            }
        }
        
        if( applicableRegistrations.size() >  0 ) {
            Map<String, Contact> mapEmailToContact = new Map<String, Contact>();    
            List<Contact> contacts = new List<Contact>([Select Id, Email 
                                                                   From Contact 
                                                                   Where Email IN :emails
                                                                   Order By CreatedDate DESC
                                                                   ]);
            if( contacts != Null && contacts.size() > 0 ){
                for( Contact con : contacts ){
                    String mapKey = con.Email.toLowerCase();
                    if( !mapEmailToContact.containsKey( mapKey )) {
                        mapEmailToContact.put( mapKey, con );  
                    }
                }
            }
        
            List<rie__Registration__c> registrationNeedsNewContact = new List<rie__Registration__c>();
            Map<Id, rie__Registration__c> registrationToUpdate = new Map<Id, rie__Registration__c>();
            Map<Id, Contact> contactsToUpsert = new Map<Id, Contact>();
            
            for(rie__Registration__c regObj : applicableRegistrations){
                String emailLowerCase = regObj.rie__Email__c.toLowerCase();
                if( mapEmailToContact.containsKey( emailLowerCase )){
                    rie__Registration__c regObjToUpdate = new rie__Registration__c( Id = regObj.Id );
                    regObjToUpdate.rie__Contact__c = mapEmailToContact.get(emailLowerCase).Id; 
                    registrationToUpdate.put( regObjToUpdate.Id, regObjToUpdate );
                    
                    Contact conToUpdate  = new Contact( Id = regObjToUpdate.rie__Contact__c );
                    conToUpdate.FMS_2019_Registration__c = regObjToUpdate.Id;
                    contactsToUpsert.put( regObjToUpdate.Id, conToUpdate );
                }
                else {
                    registrationNeedsNewContact.add( regObj );
                }
            }
            
            List<Contact> contactToInsert = new List<Contact>();
            for(rie__Registration__c regObj : registrationNeedsNewContact ){
                Contact con = new Contact();
                con.LastName = regObj.rie__Last_Name__c;
                con.Email = regObj.rie__Email__c;
                con.FMS_2019_Registration__c = regObj.Id;
                contactsToUpsert.put(regObj.Id, con);
                contactToInsert.add( con );
            }
            
            if( contactsToUpsert.size() > 0 ) {
                upsert contactsToUpsert.values();
                for( Contact con : contactToInsert ) {
                    rie__Registration__c regObjToUpdate = new rie__Registration__c( Id = con.FMS_2019_Registration__c );
                    regObjToUpdate.rie__Contact__c = con.Id; 
                    registrationToUpdate.put( regObjToUpdate.Id, regObjToUpdate );
                }
            }
            
            if( registrationToUpdate.size() > 0 ) {
                RegistrationTriggerHelper.runTrigger = false;
                update registrationToUpdate.values();
                RegistrationTriggerHelper.runTrigger = true;
            }
        }
    }    
}
Test class is given below:-
//Test class for RegisterTriggerHelper
@isTest
public class RegistrationTriggerHelperTest {
    public static testMethod void linkContactTest(){
        Contact con = new Contact(LastName = 'Scot');
        insert con;
        rie__Event__c ev = new rie__Event__c();
         
        Insert ev;
        rie__Registration__c regObj  = new rie__Registration__c();
        regObj.rie__Event__c =   ev.id;
        insert regObj;
        
    }
}
Please help me with the coverage.
 
Best Answer chosen by sumit d
sumit dsumit d
//Test class for RegisterTriggerHelper
@isTest
public class RegistrationTriggerHelperTest {
    public static testMethod void linkContactTest(){
        test.startTest();
        Contact con = new Contact();
        con.LastName = 'Scot';
        con.Email = 'test@gmail.com';
        insert con;
        rie__Event__c ev = new rie__Event__c();
        Insert ev;
        
        rie__Registration__c regObj  = new rie__Registration__c();
        regObj.rie__Email__c = 'test@gmail.com';
        regObj.rie__Event__c =   ev.id;
        insert regObj;
        test.stopTest();
        Contact cont =  [SELECT FMS_2019_Registration__c FROM Contact WHERE id =: con.id];
        rie__Registration__c regObjt = [SELECT rie__Contact__c,rie__Email__c FROM rie__Registration__c WHERE rie__Email__c =: con.Email];
        
        //assert that registration is linked with contat with same email
        System.assertEquals( con.id, regObjt.rie__Contact__c);
        System.assertEquals(  regObj.Id, cont.FMS_2019_Registration__c);
    }
    
    public static testMethod void linkNewContactTest(){
        Test.startTest();
        rie__Event__c ev = new rie__Event__c();
        Insert ev;
        rie__Registration__c regObjt  = new rie__Registration__c();
        regObjt.rie__Email__c = 'test@gmail.com';
        regObjt.rie__Last_Name__c = 'Scot';
        regObjt.rie__Event__c =   ev.id;
        insert regObjt;
        
        Contact con = new Contact();
        con.LastName = regObjt.rie__Last_Name__c;
        con.Email = regObjt.rie__Email__c;
        con.FMS_2019_Registration__c = regObjt.Id;
        upsert con;
        RegistrationTriggerHelper.runTrigger = false;
        Update regObjt;
        RegistrationTriggerHelper.runTrigger = true;
        Test.stopTest();
         
    }
}
I got the full coverage .Thanks

All Answers

Rushita Bavishi 1Rushita Bavishi 1
Hi Sumit,
I have tried to update test class code. Hope it will help you.       
@isTest
public class RegistrationTriggerHelperTest {
    public static testMethod void linkContactTest(){
        Contact con = new Contact(LastName = 'Scot');
        con.Email  = abc@test.com;
        insert con;
      
      rie__Registration__c regObj  = new rie__Registration__c();
        regObj.rie__Contact__c = con.id;
        regobj.rie__Email__c  = abc@test.com;
        insert regObj;
      
    }
}

 
Rushita Bavishi 1Rushita Bavishi 1
If it still not increasing your code coverage then put your trigger code here.
sumit dsumit d
Hi Rushita,
                  Thanks for the help. Can you help me with the other use case too where contact doesn't have the same email?
                  How can I put assert on it?
sumit dsumit d
My updated test class is given below:-
 
@isTest
public class RegistrationTriggerHelperTest {
    public static testMethod void linkContactTest(){
        
        Contact con = new Contact();
        con.LastName = 'Scot';
        con.Email = 'test@gmail.com';
        insert con;
        
        rie__Event__c ev = new rie__Event__c();
        Insert ev;
     
        rie__Registration__c regObj  = new rie__Registration__c();
        regObj.rie__Email__c = 'test@gmail.com';
        regObj.rie__Event__c =   ev.id;
        insert regObj;
        
       rie__Registration__c regObjt = [SELECT rie__Contact__c,rie__Email__c FROM rie__Registration__c WHERE rie__Email__c =: con.Email];
       System.assertEquals( con.id, regObjt.rie__Contact__c);
       
    }
}
please help me with the other test case?
sumit dsumit d
I am getting this error :-System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, Trigger_Registration: execution of AfterInsert
Rushita Bavishi 1Rushita Bavishi 1
Can You put trigger code and in which object Insertion and in which line you are getting error.
sumit dsumit d
//Trigger code
trigger Trigger_Registration on rie__Registration__c (before insert,after insert,before update,
                                                      after update,before delete,after delete) {
    RegistrationTriggerHelper.newRegistration = Trigger.new;
    RegistrationTriggerHelper.oldRegistration = Trigger.old;
    RegistrationTriggerHelper.newMapRegistration = Trigger.newMap;
    RegistrationTriggerHelper.oldMapRegistration = Trigger.oldMap;
                                                          
    if(!RegistrationTriggerHelper.runTrigger) {
        return;
    }
                                                          
    if(Trigger.isAfter){
       if(Trigger.isInsert) {
           RegistrationTriggerHelper.linkContact();
       }
    }                                                     

}
My updated test class:-
//Test class for RegisterTriggerHelper
@isTest
public class RegistrationTriggerHelperTest {
    public static testMethod void linkContactTest(){
        
        Contact con = new Contact();
        con.LastName = 'Scot';
        con.Email = 'test@gmail.com';
        insert con;
        
        rie__Event__c ev = new rie__Event__c();
        Insert ev;
        
        rie__Registration__c regObj  = new rie__Registration__c();
        regObj.rie__Email__c = 'test@gmail.com';
        regObj.rie__Event__c =   ev.id;
        insert regObj;
        
        Contact cont =  [SELECT FMS_2019_Registration__c FROM Contact WHERE id =: con.id];
        rie__Registration__c regObjt = [SELECT rie__Contact__c,rie__Email__c FROM rie__Registration__c WHERE rie__Email__c =: con.Email];
        System.assertEquals( con.id, regObjt.rie__Contact__c);
        System.assertEquals(  regObj.Id, cont.FMS_2019_Registration__c);
    }
    
    public static testMethod void linkNewContactTest(){
         rie__Event__c ev = new rie__Event__c();
        Insert ev;
        rie__Registration__c regObjt  = new rie__Registration__c();
        regObjt.rie__Email__c = 'test@gmail.com';
        regObjt.rie__Event__c =   ev.id;
        
        insert regObjt;
        
    }
}
System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, Trigger_Registration: execution of AfterInsert

caused by: System.DmlException: Upsert failed. First exception on row 0; first error: REQUIRED_FIELD_MISSING, Required fields are missing: [LastName]: [LastName]

Class.RegistrationTriggerHelper.linkContact: line 70, column 1
Trigger.Trigger_Registration: line 14, column 1: []
​​​​​​​
 on this line in helper class i am getting the error:-upsert contactsToUpsert.values();

 
sumit dsumit d
//Test class for RegisterTriggerHelper
@isTest
public class RegistrationTriggerHelperTest {
    public static testMethod void linkContactTest(){
        test.startTest();
        Contact con = new Contact();
        con.LastName = 'Scot';
        con.Email = 'test@gmail.com';
        insert con;
        rie__Event__c ev = new rie__Event__c();
        Insert ev;
        
        rie__Registration__c regObj  = new rie__Registration__c();
        regObj.rie__Email__c = 'test@gmail.com';
        regObj.rie__Event__c =   ev.id;
        insert regObj;
        test.stopTest();
        Contact cont =  [SELECT FMS_2019_Registration__c FROM Contact WHERE id =: con.id];
        rie__Registration__c regObjt = [SELECT rie__Contact__c,rie__Email__c FROM rie__Registration__c WHERE rie__Email__c =: con.Email];
        
        //assert that registration is linked with contat with same email
        System.assertEquals( con.id, regObjt.rie__Contact__c);
        System.assertEquals(  regObj.Id, cont.FMS_2019_Registration__c);
    }
    
    public static testMethod void linkNewContactTest(){
        Test.startTest();
        rie__Event__c ev = new rie__Event__c();
        Insert ev;
        rie__Registration__c regObjt  = new rie__Registration__c();
        regObjt.rie__Email__c = 'test@gmail.com';
        regObjt.rie__Last_Name__c = 'Scot';
        regObjt.rie__Event__c =   ev.id;
        insert regObjt;
        
        Contact con = new Contact();
        con.LastName = regObjt.rie__Last_Name__c;
        con.Email = regObjt.rie__Email__c;
        con.FMS_2019_Registration__c = regObjt.Id;
        upsert con;
        RegistrationTriggerHelper.runTrigger = false;
        Update regObjt;
        RegistrationTriggerHelper.runTrigger = true;
        Test.stopTest();
         
    }
}
I got the full coverage .Thanks
This was selected as the best answer