You need to sign in to do that
Don't have an account?
jule 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:
Trigger:
Test Class:
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; } }
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
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
The problem is the class test code coverage only 24%, and I don't know why ?
Thanks,
Jule
Do you have solution to have coverage 100% ?
Thanks