function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Randy HenryRandy 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%):
//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);

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;
    }
}
Best Answer chosen by Randy Henry
KaranrajKaranraj
Try the below code
 
@Istest
private class TestZipCodeAgentPopulate 
{
    static testMethod void insertLead1() 
    {
      Profile p = [SELECT Id FROM Profile WHERE Name='System Administrator']; 
      User u = new User(Alias = 'standt', Email='standarduser@testorg.com', 
      EmailEncodingKey='UTF-8', LastName='Testing', LanguageLocaleKey='en_US', 
      LocaleSidKey='en_US', ProfileId = p.Id, 
      TimeZoneSidKey='America/Los_Angeles', UserName='standarduser@testorg.com');
    
       Zip_Code__c zip = new Zip_Code__c();
       zip.Zip_Code__c = '73001';
       zip.Line_of_Business__c = 'MA Sales';
       zip.Assigned_Agent__c = U.Id;
       insert zip;

        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

ShotShot
Well, you didnt add any test, you just entered data for tests. Moreover, you forgot to update leadsToUdate list.

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();

 
KaranrajKaranraj
Try the below code
 
@Istest
private class TestZipCodeAgentPopulate 
{
    static testMethod void insertLead1() 
    {
      Profile p = [SELECT Id FROM Profile WHERE Name='System Administrator']; 
      User u = new User(Alias = 'standt', Email='standarduser@testorg.com', 
      EmailEncodingKey='UTF-8', LastName='Testing', LanguageLocaleKey='en_US', 
      LocaleSidKey='en_US', ProfileId = p.Id, 
      TimeZoneSidKey='America/Los_Angeles', UserName='standarduser@testorg.com');
    
       Zip_Code__c zip = new Zip_Code__c();
       zip.Zip_Code__c = '73001';
       zip.Line_of_Business__c = 'MA Sales';
       zip.Assigned_Agent__c = U.Id;
       insert zip;

        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;
    
  }
}

 
This was selected as the best answer
Randy HenryRandy Henry
@Karanraj Thanks for the answer. I made a few tweeks to it (I had obscured some data) and only had to add one fix. (After creating the temporary user, I needed to add an "insert u;" statement.) Long story short ... it works! Thanks again.

@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.