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
JeffroJeffro 

Apex Trigger testMethod

I currently have a trigger that updates contact information based on the Zip Code on the contact record. I also built a test method, but I am only getting to 72% instead of 75% testing coverage. Here is the trigger and testmethod...

 

Trigger:

trigger ZipData on Contact (before insert, before update) 
{
List<String> zips = new List<String>();
List<Contact> updContact = new List<Contact>();

// Loop through Contacts and create list of zip codes
for (Contact c : Trigger.new)
{
if (c.MailingPostalCode != NULL)
{
zips.add(c.MailingPostalCode);
updContact.add(c);
}
}

//query zip code table for related data
Map<String, Zip_Table__c> ContactToUpdate = new Map<String, Zip_Table__c>();

//to make sure the Map is getting set up correctly
for(Zip_Table__c z :[select Name, XCity__c, XCounty__c, XState__c, XEPS_Code__c from Zip_Table__c where Name in :zips]){
ContactToUpdate.put(z.name,z);
}



// If there is at least one match, bulk update
if (ContactToUpdate.size() > 0)
{
for (Contact c : updContact)
{
//populate the contact fields
Zip_Table__c updzip = ContactToUpdate.get(c.MailingPostalCode);
if(updzip!=null){
c.MailingCity = updzip.XCity__c;
c.MailingState = updzip.XState__c;
c.XCounty__c = updzip.XCounty__c;
c.XEPS_Code__c = updzip.XEPS_Code__c;
}
}
}else{
for (Contact c : updContact)
{
c.MailingCity = NULL;
c.MailingState = NULL;
c.XCounty__c = NULL;
c.XEPS_Code__c = NULL;
}
}
}

 testMethod:

Public class TestZipData {
static testMethod void testZip() {

Contact[] contact = new Contact[]
{new Contact(LastName = 'ZipData', MailingPostalCode = '14513'),
new Contact(LastName = 'Smith', MailingPostalCode = '12208')};

insert contact;

Contact upcontact = [select MailingPostalCode from Contact where Id = '003R000000Biw5t'];
upcontact.MailingPostalCode = '12208';

update upcontact;
}
}

 It looks as though the test is not getting to the

if (ContactToUpdate.size() > 0)

 code and below....any ideas???

 

Jeff

 

JimRaeJimRae

you are hard coding your updatecontact SOQL to a specific ID, is that what you mean to do?
Are you sure the contact with that ID even exists?

For better portability, I would recommend that you look up one of the contacts you just created.  Also, you should put a try/catch around your insert to make sure you can even insert the record.

You should also add some assertions to make sure that your trigger is actually updating the fields like you expect.

 

Here is an updated Testmethod for you:

 

 

Public class TestZipData { static testMethod void testZip() { String checkid; Contact[] contact = new Contact[] {new Contact(LastName = 'ZipData', MailingPostalCode = '14513'), new Contact(LastName = 'Smith', MailingPostalCode = '12208')}; try{ insert contact; checkid=contact.id; }catch(DMLException e){ system.assert(false,"ERROR INSERTING CONTACTS:'+e.getDMLMessage(0)); } Contact upcontact = [select MailingPostalCode from Contact where Id = :checkid]; upcontact.MailingPostalCode = '12208'; try{ update upcontact; }catch(DMLException e){ system.assert(false,"ERROR UPDATING CONTACTS:'+e.getDMLMessage(0)); } } }

 

 

 

JeffroJeffro

JimRae,

 

Even with the information you gave me in the updated testmethod, I am still coming up with 72% and it is still not covering the same lines as before.

 

Thanks,

Jeff

SuperfellSuperfell
Your test needs to insert or update a contact with no MailingPostalCode set.
JimRaeJimRae

Simon is correct about that.  You should also look at your coverage statistics when your run the test.  They will tell you exactly which lines did not get covered, then you can look to create use cases to address those specific scenarios.

 

 

JeffroJeffro

Thanks guys, that did it. Adding a new contact and inserting it without a zip put me up to 81%! Now I can use Eclipse to move it into production.

Thanks for your help,

Jeff