5

I'm having difficulties creating a Unit Test for an Apex Trigger to pass so I can push the trigger into production. I'm hoping someone can help me or point me in the right direction to where I'm falling down.

I've created a trigger to re-assign newly inserted Leads to the appropriate sales rep using a custom object (Zip_Code__c) that contains all zips, territory names, and sales reps associated. The trigger works great in the sandbox. However, I cannot port this to production because I cannot get the Unit Test to pass.

I'm obviously missing something critical in creating the Unit test class to appropriately target my code. It's been 3 years since I've touched code, and I'm definitely out of practice. :P

My Trigger:

trigger LeadAssignmentTrigger 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 Sales_Rep__c from Zip_Code__c  where Name = :lead.PostalCode limit 1];     
          // if you found one
          if (zip.size() > 0)  {   
              //assign the lead owner to the zip code owner
              lead.OwnerId = zip[0].Sales_Rep__c;         
              leadsToUpdate.add(lead);         
          }
       }
    }
}

And my Test Class

@isTest 
private class AssignLeadOnCreate{
    static testMethod void AssignLeadTest() {
        // Setup the lead record
        Lead testLead = new Lead(lastname='LAST',firstname='FIRST',company='CUSTOMER',OwnerId='00515000006ShAp',PostalCode='46038');
        System.debug('OwnerID before inserting new Lead: ' + testLead.OwnerId);
        // Insert Lead
        insert testLead;        
        // Trigger should have changed the record owner to that found in Zip_Code__c        
        // Retrieve the new Lead
        testLead = [SELECT OwnerId FROM Lead WHERE Id = :testLead.Id];
        System.debug('OwnerID after inserting new Lead: ' + testLead.OwnerId);

         //Grab ownerid for ZIP code from ZIP_code__c object
         //List<Zip_Code__c> ZipList = [select Sales_Rep__c from Zip_Code__c where Zip__c= '46038'];
         //System.debug('Size of Zip Array: ' + Ziplist.size());
         //String SalesID = ZipList[0].Sales_Rep__c;

        // String SalesID = lead.OwnerId;

        // Verify SalesID matches record owner
        //System.assertEquals('00515000006TnIN',testLead.OwnerId);
    }
}

I'm receiving 0 records back from the retrieve call. Ideally, I'd like this to work as a unit test should, but essentially, I need this to pass so I can place it into production.

I've tried many iterations of hard coding, but I'm not even good at trying to 'cheat!' :)

Any advice you can give will be much appreciated!

Best regards, Steve

2 Answers 2

6

Steve, I'm guessing the problem is that you don't have any Zip_Code__c objects defined in the test environment! Remember, tests run with no standard SObjects, they don't have access to your data. You need to manually create any objects you're going to use.

How does the following work for you?

@isTest 
private class AssignLeadOnCreate{
    static testMethod void AssignLeadTest() {
        // create the ZIP code record we need.
        Zip_Code__c testZip = new Zip_Code__c(Name='46038', Zip__c='46038', Sales_Rep__c='00515000006TnIN');
        insert testZip;
        // Setup the lead record
        Lead testLead = new Lead(lastname='LAST',firstname='FIRST',company='CUSTOMER',OwnerId='00515000006ShAp',PostalCode='46038');
        System.debug('OwnerID before inserting new Lead: ' + testLead.OwnerId);
        // Insert Lead
        insert testLead;        
        // Trigger should have changed the record owner to that found in Zip_Code__c        
        // Retrieve the new Lead
        Lead insertedLead = [SELECT Id,OwnerId FROM Lead WHERE Id = :testLead.Id];
        System.debug('OwnerID after inserting new Lead: ' + insertedLead.OwnerId);
        System.assertEquals('00515000006TnIN',insertedLead.OwnerId);

    }

}

I just ran this code with your trigger in a DE and it passed.

However, I'm seeing another issue. In your trigger, you're looking for Zip_Code__c objects where their Name equals the Lead Postal Code, but your Test Case uses the Zip__c custom field. You'll need to make sure you use the same behavior in each.

3

Christian,

Those pieces of code made several transformations since I posted the question, but the answer to pushing it to production was so simple, I feel like an idiot.

I never pushed over the test unit along with the trigger for it to properly validate! As soon as I included the Test Class, BAM! Good to go for deployment. (Obviously, this is my first forray into Test Units.)

Thank you so much for your assistance!
Best regards, Steve

2
  • +1 so you don't have to feel so badly. We've all been there as well as equivalent places. Glad you figured it out
    – cropredy
    Commented Feb 7, 2016 at 4:19
  • 1
    PS it's unit test, not test unit.
    – Adrian Larson
    Commented Feb 7, 2016 at 4:56

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .