You need to sign in to do that
Don't have an account?
AntonyWarc
How to build a test calss for my trigger
I have no Apex coding experience, can someone help me write a test class for the following Apex Trigger?
trigger updateCCLookupField on Lead (before insert, before update) {
List<Lead> leads = new List<Lead>();
for (Lead l : Trigger.new)
{
Try
{
Campaign_Codes__c AssociatedCC = [SELECT Id FROM Campaign_Codes__c WHERE CodeOnCC__c= :l.Campaign_Code__c];
l.CC_Lookup__c = AssociatedCC.Id;
}
Catch(Exception e)
{
l.CC_Lookup__c = 'a0zD00000028X59';
}
}
}
@isTest
private class AUMTriggerHelperTest {
// This is for insert
static testMethod void testLeadMehod1() {
// Create Custom object
Campaign_Codes__c testccode = new Campaign_Codes__c();
// Fill teh reuired fields
insert testccode ;
// Create Lead
Lead testLead = new Lead();
testLead.Campaign_Code__c = testccode. CodeOnCC__c
//fill the required fields
insert testLead;
// System Assertion
System.assertEquals(testLead.CC_Lookup__c,testccode.id);
}
// For update
static testMethod void testLeadMehod1() {
// Create Lead
Lead testLead = new Lead();
//fill the required fields
insert testLead;
// Create Custom object
Campaign_Codes__c testccode = new Campaign_Codes__c();
// Fill teh reuired fields
insert testccode ;
// update lead
testLead.Campaign_Code__c = testccode. CodeOnCC__c;
update testLead;
// System Assertion
System.assertEquals(testLead.CC_Lookup__c,testccode.id);
}
Similar;y test in bulk mode - multiple leads and custom objects update and insertion
positive test classes - If all values are there
negative test cases - if null values in the fields
All Answers
@isTest
private class AUMTriggerHelperTest {
// This is for insert
static testMethod void testLeadMehod1() {
// Create Custom object
Campaign_Codes__c testccode = new Campaign_Codes__c();
// Fill teh reuired fields
insert testccode ;
// Create Lead
Lead testLead = new Lead();
testLead.Campaign_Code__c = testccode. CodeOnCC__c
//fill the required fields
insert testLead;
// System Assertion
System.assertEquals(testLead.CC_Lookup__c,testccode.id);
}
// For update
static testMethod void testLeadMehod1() {
// Create Lead
Lead testLead = new Lead();
//fill the required fields
insert testLead;
// Create Custom object
Campaign_Codes__c testccode = new Campaign_Codes__c();
// Fill teh reuired fields
insert testccode ;
// update lead
testLead.Campaign_Code__c = testccode. CodeOnCC__c;
update testLead;
// System Assertion
System.assertEquals(testLead.CC_Lookup__c,testccode.id);
}
Similar;y test in bulk mode - multiple leads and custom objects update and insertion
positive test classes - If all values are there
negative test cases - if null values in the fields