Trigger:
trigger CountyLookupByZip on Contact (before insert) {
for (Contact c : Trigger.New) {
if(c.MailingPostalCode != null) {
List<Zip_Code__c> contactCounty = new List<Zip_Code__c>();
contactCounty = [SELECT Id,
County__c,
Postal_Code__c
FROM Zip_Code__c
WHERE Postal_Code__c = :c.MailingPostalCode];
if(contactCounty.size() != 0){
c.Mailing_County__c = contactCounty[0].County__c;
} else {
c.MailingPostalCode.addError('Postal Code not found. Please ensure the postal code is a valid Virginia zip code.');
}
}
}
}
Test Clas:
@isTest
public class TestCountyLookupByZip {
@isTest
public static void TestContactWithNullZip() {
// Test data setup
// Create a contact without a Mailing Postal Code
// and check to ensure County is also null
Contact contactNullZip = new Contact(LastName = 'Test');
insert contactNullZip;
// Perform Test
System.assertEquals(contactNullZip.Mailing_County__c, null);
}
@isTest
public static void TestContactWithValidZip() {
// Test data setup
// Create a contact with a valid VA Mailing Postal Code
// and check to ensure County is updated correctly
Contact contactValidZip = new Contact(LastName = 'Test', MailingPostalCode = '20101');
insert contactValidZip;
// Perform Test
System.assertEquals(contactValidZip.Mailing_County__c, 'Loudoun');
}
@isTest
public static void TestContactWithInvalidZip() {
// Test data setup
// Create a contact with an invalid Mailing Postal Code
// and throw an error
Test.startTest();
try {
Contact contactInvalidZip = new Contact(LastName = 'Test', MailingPostalCode = '17602');
insert contactInvalidZip;
// Perform Test
} catch(Exception e) {
System.Assert(e.getMessage().contains('FIELD_CUSTOM_VALIDATION_EXCEPTION'));
System.Assert(e.getMessage().contains('Postal Code not found. Please ensure the postal code is a valid Virginia zip code.'));
}
Test.stopTest();
}
}

Hi StephenYou still get the error as you have not re-queried the record that you have created. If you go to this thread https://developer.salesforce.com/forums/ForumsMain?id=9062I000000IKZFQA4and search for "why we grab the written record in our Apex Test Classes" you will see a post that will explain what is required and why. But if you want the short version, update your method to:
RegardsAndrew@isTest
public static void TestContactWithValidZip() {
// Test data setup
// Create a contact with a valid VA Mailing Postal Code
// and check to ensure County is updated correctly
Zip_Code__c zipCode = new Zip_Code__c(Postal_Code__c = '20101', County__c = 'Loudoun');
insert zipCode;
Contact contactValidZip = new Contact(LastName = 'Test', MailingPostalCode = '20101');
insert contactValidZip;
Contact insertedContact = [SELECT Id, Mailing_County__c FROM Contact WHERE Id = :contactValidZip.Id LIMIT 1];
// Perform Test
//general format for asserts is expected v actual so
System.assertEquals('Loudoun', insertedContact.Mailing_County__c, );
}