+ Start a Discussion
jule robinsonjule robinson 

Test class for triggerHandler and Trigger after update

Hi,
I need help to create test class for a TriggerHandler Class.
I will show you my code.
Class Trigger Handler:
public class ContactTriggerHandler {
    
    Public static void newTasks_VisitPlanner(List<Contact> newContacts, Map<Id,Contact> mapOldContacts) {
        
        
        List<Task> NewTasks = new List<Task>();
        for (Contact con: newContacts){
            If(!checkRecursive.SetOfIDs.contains(con.Id)){
                Contact oldContact = mapOldContacts.get(con.Id);
                if(con.Frequency__c != oldContact.Frequency__c){
                    
                    if (con.Frequency__c == '3 Months'){
                        for(Integer i = 1; i < 5; i++){
                            Task newTask = new Task(
                                WhoId = con.Id,
                                WhatId = con.AccountId,
                                OwnerId = con.Assigned_to__c,
                                Subject = 'Rappel : Visite périodique',
                                ActivityDate = Date.today().addMonths(i*3),
                                Priority = 'Normal',
                                Status = 'Not Started',
                                Created_from_Visit_planner__c = true
                            );
                            NewTasks.add(newTask);
                        }
                    }
                    
                    if (con.Frequency__c == '6 Months'){
                        for(Integer i = 1; i < 3; i++){
                            Task newTask = new Task(
                                WhoId = con.Id,
                                WhatId = con.AccountId,
                                OwnerId = con.Assigned_to__c,
                                Subject = 'Rappel : Visite périodique',
                                ActivityDate = Date.today().addMonths(i*6),
                                Priority = 'Normal',
                                Status = 'Not Started',
                                Created_from_Visit_planner__c = true
                            );
                            NewTasks.add(newTask);
                        }
                    }
                }
                checkRecursive.SetOfIDs.add(con.Id);
            }
        }
        
        
        if(NewTasks != null && NewTasks.size()>0){
            insert NewTasks;
        }    
    }
}

Trigger:
trigger NewTasks_VisitPlanner_Trigger on Contact (after update) {
    
    ContactTriggerHandler.newTasks_VisitPlanner(Trigger.new, Trigger.oldMap);
}

Test Class:
@isTest
private class Test_NewTasksVisit_Trigger {
    
    @isTest
    public static void Test_newsTask3Months(){
        Account acc = new Account(Name = 'OPALE', Type = 'Public sector', Main_Role__c = 'Contracting authority' );
        insert acc;
        
        Contact con_A = new Contact(FirstName='Albert',LastName='Roche', AccountId = acc.id);
        insert con_A;

        con_A.Frequency__c='3 Months';
        con_A.Assigned_to__c=con_A.Name;
        update con_A;
        
        con_A.Frequency__c='6 Months';
        con_A.Assigned_to__c=con_A.Name;
        update con_A;
    }
    
    @isTest
    public static void Test_newsTask6Months(){
        Account acc = new Account(Name = 'SOGEA', Type = 'Private sector', Main_Role__c = 'Contracting authority support' );
        insert acc;
        
        Contact con_B = new Contact(FirstName='Jean',LastName='Rabi', AccountId = acc.id);
        insert con_B;
        
        con_B.Frequency__c='6 Months';
        con_B.Assigned_to__c=con_B.Name;
        update con_B;
        
        con_B.Frequency__c='3 Months';
        con_B.Assigned_to__c=con_B.Name;
        update con_B;
    }
}

​​​​​​​​​​​​​​
Best Answer chosen by jule robinson
ManojjenaManojjena
HI  jule robinson,

Whats the problem you are facing ?   Try to floow below links it will help .

http://manojjena20.blogspot.com/2015/06/tips-and-tricks-for-test-class.html (https://trailhead.salesforce.com/en/content/learn/modules/apex_testing/apex_testing_intro)
https://trailhead.salesforce.com/en/content/learn/modules/apex_testing/apex_testing_intro

Try to run the test class in you rorganisation , you can install code coverage calculator chrome extension which will help you to identify the line which covered .
Thanks ,
Manoj

 

All Answers

ManojjenaManojjena
HI  jule robinson,

Whats the problem you are facing ?   Try to floow below links it will help .

http://manojjena20.blogspot.com/2015/06/tips-and-tricks-for-test-class.html (https://trailhead.salesforce.com/en/content/learn/modules/apex_testing/apex_testing_intro)
https://trailhead.salesforce.com/en/content/learn/modules/apex_testing/apex_testing_intro

Try to run the test class in you rorganisation , you can install code coverage calculator chrome extension which will help you to identify the line which covered .
Thanks ,
Manoj

 
This was selected as the best answer
jule robinsonjule robinson
Hi,
The problem is the class test code coverage only 24%, and I don't know why ?
Thanks,
Jule
jule robinsonjule robinson
Hi, 
Do you have solution to have coverage 100% ?
Thanks