You need to sign in to do that
Don't have an account?
Alex Skempris
test class for insert trigger
Hello,
I'm trying to write a test class for a simple before insert trigger and I can't for the life of me figure out what i'm missing or if I'm in the wrong track completely (i'm an apex beginner). My code below:
trigger Update_Hol_to_Wyb_Fields on Account (before insert) {
for (Account wyb:Trigger.New){
if (wyb.RecordTypeId == '01258000000VBdv' && wyb.PersonLeadSource == 'Holkham'){
List <Account> holkhamAccount = [SELECT DOB__c, BillingStreet, BillingCity, BillingState, BillingCountry from Account WHERE RecordTypeId = '012580000005mIR' AND FirstName =: wyb.FirstName AND LastName =: wyb.LastName LIMIT 1];
try{
wyb.DOB__c = holkhamAccount[0].DOB__c;
wyb.BillingStreet = holkhamAccount[0].BillingStreet;
wyb.BillingCity = holkhamAccount[0].BillingCity;
wyb.BillingState = holkhamAccount[0].BillingState;
wyb.BillingCountry = holkhamAccount[0].BillingCountry;
}
Catch (exception e){
wyb.adderror(e.getmessage()+' Please contact Alex');
}
}
}
}
My (unsuccessful) test class attempt:
@isTest
public class Update_Hol_to_Wyb_FieldsTestClass
{
Static testMethod void myUnitTest()
{
// Create the data
Account wyb = new Account();
wyb.FirstName = 'alex';
wyb.LastName = 'Test';
wyb.RecordTypeId = '01258000000VBdv';
insert wyb;
List <Account> holkhamAccount = [SELECT DOB__c, BillingStreet, BillingCity, BillingState, BillingCountry from Account WHERE RecordTypeId = '012580000005mIR' AND FirstName =: wyb.FirstName AND LastName =: wyb.LastName LIMIT 1];
Account hol = new Account();
hol.FirstName = 'alex';
hol.LastName = 'Test';
hol.RecordTypeId = '012580000005mIR';
hol.BillingStreet = holkhamAccount[0].BillingStreet;
hol.BillingCity = holkhamAccount[0].BillingCity;
hol.BillingState = holkhamAccount[0].BillingState;
hol.BillingCountry = holkhamAccount[0].BillingCountry;
hol.DOB__c = holkhamAccount[0].DOB__c;
insert hol;
Test.startTest();
insert wyb;
Test.stopTest();
List <Account> wybAccount = [SELECT DOB__c, BillingStreet, BillingCity, BillingState, BillingCountry from Account WHERE RecordTypeId = '01258000000VBdv' AND FirstName =: wyb.FirstName AND LastName =: wyb.LastName AND PersonLeadSource =: wyb.PersonLeadSource];
system.assertEquals(1, wybAccount.size());
system.assertEquals(wybAccount[0].BillingCity, hol.BillingCity);
system.assertEquals(wybAccount[0].BillingStreet, hol.BillingStreet);
system.assertEquals(wybAccount[0].BillingState, hol.BillingState);
system.assertEquals(wybAccount[0].BillingCountry, hol.BillingCountry);
system.assertEquals(wybAccount[0].DOB__c, hol.DOB__c);
}
}
Any help would be greatly appreciated.
Thanks
Alex
I'm trying to write a test class for a simple before insert trigger and I can't for the life of me figure out what i'm missing or if I'm in the wrong track completely (i'm an apex beginner). My code below:
trigger Update_Hol_to_Wyb_Fields on Account (before insert) {
for (Account wyb:Trigger.New){
if (wyb.RecordTypeId == '01258000000VBdv' && wyb.PersonLeadSource == 'Holkham'){
List <Account> holkhamAccount = [SELECT DOB__c, BillingStreet, BillingCity, BillingState, BillingCountry from Account WHERE RecordTypeId = '012580000005mIR' AND FirstName =: wyb.FirstName AND LastName =: wyb.LastName LIMIT 1];
try{
wyb.DOB__c = holkhamAccount[0].DOB__c;
wyb.BillingStreet = holkhamAccount[0].BillingStreet;
wyb.BillingCity = holkhamAccount[0].BillingCity;
wyb.BillingState = holkhamAccount[0].BillingState;
wyb.BillingCountry = holkhamAccount[0].BillingCountry;
}
Catch (exception e){
wyb.adderror(e.getmessage()+' Please contact Alex');
}
}
}
}
My (unsuccessful) test class attempt:
@isTest
public class Update_Hol_to_Wyb_FieldsTestClass
{
Static testMethod void myUnitTest()
{
// Create the data
Account wyb = new Account();
wyb.FirstName = 'alex';
wyb.LastName = 'Test';
wyb.RecordTypeId = '01258000000VBdv';
insert wyb;
List <Account> holkhamAccount = [SELECT DOB__c, BillingStreet, BillingCity, BillingState, BillingCountry from Account WHERE RecordTypeId = '012580000005mIR' AND FirstName =: wyb.FirstName AND LastName =: wyb.LastName LIMIT 1];
Account hol = new Account();
hol.FirstName = 'alex';
hol.LastName = 'Test';
hol.RecordTypeId = '012580000005mIR';
hol.BillingStreet = holkhamAccount[0].BillingStreet;
hol.BillingCity = holkhamAccount[0].BillingCity;
hol.BillingState = holkhamAccount[0].BillingState;
hol.BillingCountry = holkhamAccount[0].BillingCountry;
hol.DOB__c = holkhamAccount[0].DOB__c;
insert hol;
Test.startTest();
insert wyb;
Test.stopTest();
List <Account> wybAccount = [SELECT DOB__c, BillingStreet, BillingCity, BillingState, BillingCountry from Account WHERE RecordTypeId = '01258000000VBdv' AND FirstName =: wyb.FirstName AND LastName =: wyb.LastName AND PersonLeadSource =: wyb.PersonLeadSource];
system.assertEquals(1, wybAccount.size());
system.assertEquals(wybAccount[0].BillingCity, hol.BillingCity);
system.assertEquals(wybAccount[0].BillingStreet, hol.BillingStreet);
system.assertEquals(wybAccount[0].BillingState, hol.BillingState);
system.assertEquals(wybAccount[0].BillingCountry, hol.BillingCountry);
system.assertEquals(wybAccount[0].DOB__c, hol.DOB__c);
}
}
Any help would be greatly appreciated.
Thanks
Alex
I have managed to get the coverage up to 100%. Ironically the only thing I was missing was a (seeAllData = True) statement on top of the class. That's why the query was fetching no results.
I'm attaching the updated test class for anyone bumping on this thread.
@isTest (seeAllData = true)
public class Update_Hol_to_Wyb_FieldsTestClass
{
Public string FirstName2{get; set;}
Static testMethod void myUnitTest() // test for passing the fields over
{
// Create the data
Account wyb = new Account();
wyb.FirstName = 'Alex';
wyb.LastName = 'test2';
wyb.RecordTypeId = '01258000000VBdv';
wyb.PersonLeadSource = 'Holkham';
Account hol = new Account();
hol.FirstName = 'Alex';
hol.LastName = 'test2';
hol.RecordTypeId = '012580000005mIR';
hol.DOB__c = Date.newInstance(1984, 12, 9);
hol.BillingStreet = 'test Street';
hol.BillingCity = 'test City';
hol.BillingState = 'test state';
hol.BillingCountry = 'test country';
insert hol;
Test.startTest();
insert wyb;
test.stopTest();
}
Static testMethod void myUnitTest2() // Test for the exception - notice the change the wyb.lastname
{
// Create the data
Account wyb = new Account();
wyb.FirstName = 'Alex';
wyb.LastName = 'test3';
wyb.RecordTypeId = '01258000000VBdv';
wyb.PersonLeadSource = 'Holkham';
Account hol = new Account();
hol.FirstName = 'Alex';
hol.LastName = 'test2';
hol.RecordTypeId = '012580000005mIR';
hol.DOB__c = Date.newInstance(1984, 12, 9);
hol.BillingStreet = 'test Street';
hol.BillingCity = 'test City';
hol.BillingState = 'test state';
hol.BillingCountry = 'test country';
insert hol;
Test.startTest();
insert wyb;
test.stopTest();
}
}
All Answers
Apologies. My problem is that the code coverage is only at 18%.
Thanks
Alex
From the list down pretty much... all the lines below are still on the red
List <Account> holkhamAccount = [SELECT DOB__c, BillingStreet, BillingCity, BillingState, BillingCountry from Account WHERE RecordTypeId = '012580000005mIR' AND FirstName =: wyb.FirstName AND LastName =: wyb.LastName LIMIT 1];
try{
wyb.DOB__c = holkhamAccount[0].DOB__c;
wyb.BillingStreet = holkhamAccount[0].BillingStreet;
wyb.BillingCity = holkhamAccount[0].BillingCity;
wyb.BillingState = holkhamAccount[0].BillingState;
wyb.BillingCountry = holkhamAccount[0].BillingCountry;
}
Catch (exception e){
wyb.adderror(e.getmessage()+' Please contact Alex');
}
}
}
}
// Create the data
Account wyb = new Account();
wyb.FirstName = 'alex';
wyb.LastName = 'Test';
wyb.RecordTypeId = '01258000000VBdv';
wyb.PersonLeadSource = 'Holkham';
insert wyb;
Please mark as solved if this helps you
Good luck !
wyb.BillingStreet = holkhamAccount[0].BillingStreet;
wyb.BillingCity = holkhamAccount[0].BillingCity;
wyb.BillingState = holkhamAccount[0].BillingState;
wyb.BillingCountry = holkhamAccount[0].BillingCountry;
Thanks
Alex
wyb.FirstName = 'alex';
wyb.LastName = 'Test';
wyb.RecordTypeId = '01258000000VBdv';
wyb.PersonLeadSource = 'Holkham';
wyb.BillingStreet ='test Street';
wyb.BillingCity = 'test City';
wyb.BillingState = 'test State';
wyb.BillingCountry = 'test Country';
insert wyb;
So it seems that it's only covering one field rather than the DOB__c field specifically.
Thanks
Alex
You are exactly right. I'm landing in my exception block, I've confirmed the query returns no results. Only thing is I've confirmed there is an account in my sandbox org with those first and last names in and the query still returns nothing. Trying to troubleshoot now.
Thanks
Alex
or used in your SOQL where clause
I have managed to get the coverage up to 100%. Ironically the only thing I was missing was a (seeAllData = True) statement on top of the class. That's why the query was fetching no results.
I'm attaching the updated test class for anyone bumping on this thread.
@isTest (seeAllData = true)
public class Update_Hol_to_Wyb_FieldsTestClass
{
Public string FirstName2{get; set;}
Static testMethod void myUnitTest() // test for passing the fields over
{
// Create the data
Account wyb = new Account();
wyb.FirstName = 'Alex';
wyb.LastName = 'test2';
wyb.RecordTypeId = '01258000000VBdv';
wyb.PersonLeadSource = 'Holkham';
Account hol = new Account();
hol.FirstName = 'Alex';
hol.LastName = 'test2';
hol.RecordTypeId = '012580000005mIR';
hol.DOB__c = Date.newInstance(1984, 12, 9);
hol.BillingStreet = 'test Street';
hol.BillingCity = 'test City';
hol.BillingState = 'test state';
hol.BillingCountry = 'test country';
insert hol;
Test.startTest();
insert wyb;
test.stopTest();
}
Static testMethod void myUnitTest2() // Test for the exception - notice the change the wyb.lastname
{
// Create the data
Account wyb = new Account();
wyb.FirstName = 'Alex';
wyb.LastName = 'test3';
wyb.RecordTypeId = '01258000000VBdv';
wyb.PersonLeadSource = 'Holkham';
Account hol = new Account();
hol.FirstName = 'Alex';
hol.LastName = 'test2';
hol.RecordTypeId = '012580000005mIR';
hol.DOB__c = Date.newInstance(1984, 12, 9);
hol.BillingStreet = 'test Street';
hol.BillingCity = 'test City';
hol.BillingState = 'test state';
hol.BillingCountry = 'test country';
insert hol;
Test.startTest();
insert wyb;
test.stopTest();
}
}
http://www.laceysnr.com/seealldata-why-i-think-you-shouldnt-use/
good luck !