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
AlSawtoothAlSawtooth 

Need help with test coverage for very simple trigger

Here is my trigger (thanks to help from Ashlekh (https://developer.salesforce.com/forums/ForumsProfile?communityId=09aF00000004HMG&userId=005F0000003FjAE&showHeader=false)!):

trigger UpdateEmployerAddress on Contact (before insert, before update)
{
    Set<String> setoforgs = new Set<String>();
    for (Contact inloop : Trigger.new){
        if(inloop.Employer_Lookup__c !=null){
            setoforgs.add(inloop.Employer_Lookup__c);}
    }
    Map<id,Organization__c> orgs = new Map<id,Organization__c>( [SELECT Id, Mailing_Street__c, Mailing_City__c, Mailing_State__c, Mailing_Zip_Code__c, Mailing_Country__c
                                            FROM Organization__c  WHERE Id in :setoforgs]);
 
    for (Contact inloop : Trigger.new){
        if(inloop.Employer_Lookup__c !=null){
              
          if(orgs.size()>0 &&  orgs.containsKey(inloop.Employer_Lookup__c)){
              Organization__c o = orgs.get(inloop.Employer_Lookup__c);
              inloop.cv__Employer_Street__c = o.Mailing_Street__c;
              inloop.cv__Employer_City__c = o.Mailing_City__c;
              inloop.cv__Employer_State__c = o.Mailing_State__c;
              inloop.cv__Employer_Postal_Code__c = o.Mailing_Zip_Code__c;
              inloop.cv__Employer_Country__c = o.Mailing_Country__c;
          }
        }
    }
}

Here is my test class, but I'm only getting 43% coverage. What else can I add to the test class to cover the rest of the trigger?
@isTest
private class UpdateEmployerAddressTest {

    static testMethod void EmpTestMethod() {
       Organization__c o1 = new Organization__c();
       o1.Name ='Test Org';
       o1.Mailing_Street__c = '123 Main Street';
       o1.Mailing_City__c = 'Brooklyn';
       insert (o1);

       Contact cons = new Contact();
       cons.LastName = 'Tester';
       cons.cv__Employer__c='Test Org';    
       insert(cons);
       
       Contact cons2 = new Contact();
       cons2.LastName = 'Tester2';
       cons2.cv__Employer__c = 'Nope';
       insert(cons2);
       
       System.assertNotEquals(o1.Id, cons2.Employer_Lookup__c);
       System.assertNotEquals(o1.Mailing_City__c, cons2.cv__Employer_City__c);
      }
    }

Thanks!!
James LoghryJames Loghry
Looks like you need to specify "Employeer_Lookup__c" for your contact record(s).  

For instance add the following to between 13/14:

cons.Employeer_Lookup__c = o1.Id;

That should drastically increase your code coverage.
AlSawtoothAlSawtooth
Thanks James! That didn't do me much good (I'm up to 47%) - I have another trigger in my instance that takes Employer (text) and fills in Employer Lookup (lookup), which I can tell is working in the debug log.

Do you see any other reason for such low coverage? And thank you!
AlSawtoothAlSawtooth
Figured it out: just needed to include State, Zip, and Country in the test class org.