You need to sign in to do that
Don't have an account?
Randy Henry
Apex Text Class against a SOQL query
I am running into test coverage issues when trying to get my new Apex Trigger configured. The issue is with code that is strictly a SOQL query and then placement of the value in the DB. The code takes a value (a User ID from a custom object) and looks it up in the User object. It then finds the Name and places that in a field on the Lead record.
Here is the code that I can't seem to get covered (I'm currently at 72%):
I have the rest of the code working fine. Basically I create a new lead, put in all of the required fields, and submit. It bypasses this code every time.
Here is my trigger:
trigger Zip_Code_Agent_Populate on Lead (before insert)
{
List<Lead> leadsToUpdate = new List<Lead>();
for (Lead lead : Trigger.new)
{
if (lead.PostalCode != NULL)
{
// Find the sales rep for the current zip code
List <Zip_Code__c> zip = [select Assigned_Agent__c from Zip_Code__c
where Zip_Code__c = :lead.PostalCode
and Line_of_Business__c = :lead.Lead_Line_of_Business__c
limit 1];
// if you found one
if (zip.size() > 0)
{
//find the name literal from the user ID
List<User> agent = [SELECT Name FROM User WHERE Id =: zip[0].Assigned_Agent__c limit 1];
//assign the lead owner suggestion to the agent for zip code field
lead.Agent_for_Zip_Code__c = agent[0].Name;
leadsToUpdate.add(lead);
}
}
}
}
Here is the test class:
@Istest
private class TestZipCodeAgentPopulate
{
static testMethod void insertLead1()
{
Lead l = new Lead();
l.FirstName = 'First';
l.LastName = 'Last';
l.Product_Line__c = 'MA';
l.Sub_Product_Line__c = 'Gen';
l.LeadSource = 'Network';
l.Permission_to_Contact__c = 'Yes';
l.Status = 'New';
l.Lead_Line_of_Business__c = 'MA Sales';
l.PostalCode = '73001';
insert l;
}
}
Here is the code that I can't seem to get covered (I'm currently at 72%):
//find the name literal from the user ID
List<User> agent = [SELECT Name FROM User WHERE Id =: zip[0].Assigned_Agent__c limit 1];
//assign the lead owner suggestion to the agent for zip code field
lead.Agent_for_Zip_Code__c = agent[0].Name;
leadsToUpdate.add(lead);
List<User> agent = [SELECT Name FROM User WHERE Id =: zip[0].Assigned_Agent__c limit 1];
//assign the lead owner suggestion to the agent for zip code field
lead.Agent_for_Zip_Code__c = agent[0].Name;
leadsToUpdate.add(lead);
I have the rest of the code working fine. Basically I create a new lead, put in all of the required fields, and submit. It bypasses this code every time.
Here is my trigger:
trigger Zip_Code_Agent_Populate on Lead (before insert)
{
List<Lead> leadsToUpdate = new List<Lead>();
for (Lead lead : Trigger.new)
{
if (lead.PostalCode != NULL)
{
// Find the sales rep for the current zip code
List <Zip_Code__c> zip = [select Assigned_Agent__c from Zip_Code__c
where Zip_Code__c = :lead.PostalCode
and Line_of_Business__c = :lead.Lead_Line_of_Business__c
limit 1];
// if you found one
if (zip.size() > 0)
{
//find the name literal from the user ID
List<User> agent = [SELECT Name FROM User WHERE Id =: zip[0].Assigned_Agent__c limit 1];
//assign the lead owner suggestion to the agent for zip code field
lead.Agent_for_Zip_Code__c = agent[0].Name;
leadsToUpdate.add(lead);
}
}
}
}
Here is the test class:
@Istest
private class TestZipCodeAgentPopulate
{
static testMethod void insertLead1()
{
Lead l = new Lead();
l.FirstName = 'First';
l.LastName = 'Last';
l.Product_Line__c = 'MA';
l.Sub_Product_Line__c = 'Gen';
l.LeadSource = 'Network';
l.Permission_to_Contact__c = 'Yes';
l.Status = 'New';
l.Lead_Line_of_Business__c = 'MA Sales';
l.PostalCode = '73001';
insert l;
}
}
All Answers
Now you have to check different scenarios of behave of your code. For instance, what will be if:
PostalCode is NULL, what will you get?
If not, what will you get?
...
And compare result with your expectations with using System.assert();
@Bogdon Kulyk I added the NULL case. I come from a manual testing background and was never a developer beyond some automated testing suites. Most of what you mentioned I have already tested on the front end.