Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Update_Current_Job_Title_Test
// Assuming trigger is:
trigger Update_Current_job_Title_onContact on Career_Path__c (After insert, After update) {
if((trigger.isInsert || trigger.isUpdate) && trigger.isAfter)
{
Update_Current_Job_Title.WhenInsertNewCareerPathOrUpdate(trigger.new);
}
}
// Assuming class is:
public class Update_Current_Job_Title {
public static void WhenInsertNewCareerPathOrUpdate(List<Career_Path__c> careerPathListNew)
{
try{
List<Career_Path__c> careerPathList = new List<Career_Path__c>();
List<contact> contactList = new List<contact>();
List<contact> contactListUpdated = new List<contact>();
set<id> IdList = new set<Id>();
careerPathList = [select id, contact__c , Job_title_2__c,Current__c
from Career_Path__c
where id in:careerPathListNew
and Job_title_2__c !=null];
for(Career_Path__c cp :careerPathList )
{
IdList.add(cp.contact__c);
}
contactList = [select id, Current_Job_Title__c from contact where id in : IdList];
for(Career_Path__c cpp :careerPathList )
{
for(contact cc :contactList )
{
if(cpp.Contact__c == cc.id && cpp.Current__c==true)
{
cc.Current_Job_Title__c = cpp.Job_Title_2__c;
contactListUpdated.add(cc);
}
}
}
update contactListUpdated;
} catch(Exception e)
{
system.debug('Error is -> '+ e.getMessage() + 'at Line -> '+ e.getLineNumber());
}
}
}
// Then a test class that 'covers' 75% of the lines of your code is required.
// By 'covers', we mean 'uses'. That's all that's technically required. Some test code that makes use of your code.
// So, what does that look like? Well, the simplest version for us here is creating and updating a Career_Path__c record.
// We know that our trigger fires every time we insert and update a record, so we can start there:
@isTest
private class Update_Current_Job_Title_Test {
static testMethod void testInsertAndUpdate() {
// Given
Contact__c contact = new Contact__c(
Current_Job_Title__c = 'oldTitle'
);
insert contact;
Career_Path__c careerPath = new Career_Path__c (
Contact__c = contact.Id,
Job_Title_2__c = 'newTitle'
);
insert careerPath;
// When
careerPath.Current__c = true;
update careerPath;
}
}
// There's one major problem with this test class.
// Even if it works, and covers 75% of your code, it doesn't 'know' that it works.
// All it checks is that it didn't fail so badly that an Exception was thrown.
// So, we include a 'then' condition. Something that asserts our code did what we intended.
// Specifically, we need to check if the all the related Contact__c records have had their Job Title updated.
@isTest
private class Update_Current_Job_Title_Test {
static testMethod void testInsertAndUpdate() {
// Given
Contact__c contact = new Contact__c(
Current_Job_Title__c = 'oldTitle'
);
insert contact;
Career_Path__c careerPath = new Career_Path__c (
Contact__c = contact.Id,
Job_Title_2__c = 'newTitle'
);
insert careerPath;
// When
careerPath.Current__c = true;
update careerPath;
// Then
contact = [SELECT Current_Job_Title__c FROM Contact__c WHERE Id :contact.Id LIMIT 1];
System.assertEquals('newTitle', contact.Current_Job_Title__c);
}
}
// If everything worked, then our test class has successfully:
// - created a linked Contact__c and Career_Path__c
// - set the Career Path's Job Title to 'oldTitle'
// - updated the Career Path to be Current, triggering your trigger
// - requeried for the update Contact
// - tested that the Contact's Job Title is now 'newTitle'
// There's some other points to make here, but I'll leave them out for now.
// Please comment/ message when you reach this point with the results of the test class.
// Feel free to tweak it as required, especially where the test records being created have required fields.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.