+ Start a Discussion
tsalbtsalb 

Test - Assertion Failed: Expected:null

I need some help wrapping my head around testing this trigger - I seem to have prepared and inserted my dataset correct, but I'm not sure why it's giving me an error of "Expected:Null".

 

My Trigger (working in sandbox, manually inserting records)

 

trigger AddCountyId on Contact (before insert, before update) {
	
    Set<String> CountyIds = new Set<String>();
    
    for(Contact c : Trigger.new) {       
       CountyIds.add(c.fdicCountyID__c);       
    }       
    
    Map<String, Id> countyMap = new Map<String,Id>();

	//Add both sets of IDS to above Map
	for(County__c cty : [SELECT cty.Id, cty.fdicCountyID__c
		FROM County__c cty
		WHERE cty.fdicCountyID__c != null AND cty.fdicCountyID__c IN :CountyIds]) {
    		countyMap.put(cty.fdicCountyID__c, cty.Id);
		}    
  
  	//Update Primary County on the Contact with County ID
    for(Contact updateC : Trigger.new) {
	    try {    
	          if(updateC.fdicCountyID__c != null) {
	  			  //Update Primary County with County Id from Map
	              updateC.Primary_County__c = countyMap.get(updateC.fdicCountyID__c);
	          }
	    }  
	    catch (Exception e) {
	   	}
	}

}

 

Stripped down test method in my test class

 

	static testMethod void verifyCountyId() {
		
		//Create Test County
  		County__c cty = new County__c(Name='Test County', Latitude__c = 1, Longitude__c = 1, fdicCountyId__c ='abcd1234');
  		insert cty;
		
		//Create Test Vendor
		Account vAcct = new Account(Name = 'Test Vendor Account');
		insert vAcct;
		Contact v = new Contact(LastName = 'Vendor Demo', last_engaged_date__c = Date.today(), AccountId = vAcct.Id, fdicCountyId__c = 'abcd1234');
		insert v;
		
		System.assertEquals (v.Primary_County_ID__c, cty.Id);
	}

 I get a System.AssertException: Assertion Failed: Expected:null Actual: a0hJ000000FymvMac 

 

Does anyone have any insight as to what i'm doing wrong?

Best Answer chosen by Admin (Salesforce Developers) 
dmchengdmcheng

When you insert the contact variable v, you get the ID back automatically, but you don't get any other field changes, so Primary_County_ID__c will be empty.

 

You need to requery the contact and specifically retrieve the Primary_County_ID__c field so you can compare it.  You could do something like

Contact result = [select Primary_County_ID__c from Contact where Id = :v.Id];
systemAssertEquals(result.Primary_County_ID__c, cty.Id);

 

All Answers

dmchengdmcheng

When you insert the contact variable v, you get the ID back automatically, but you don't get any other field changes, so Primary_County_ID__c will be empty.

 

You need to requery the contact and specifically retrieve the Primary_County_ID__c field so you can compare it.  You could do something like

Contact result = [select Primary_County_ID__c from Contact where Id = :v.Id];
systemAssertEquals(result.Primary_County_ID__c, cty.Id);

 

This was selected as the best answer
tsalbtsalb

Thanks sir, did not know a query was necessary in this case. I had typed the wrong field as well, but this is snippet is working:

 

	static testMethod void verifyCountyId() {
		
		//Create Test County
  		County__c cty = new County__c(Name='Test County', Latitude__c = 1, Longitude__c = 1, fdicCountyId__c ='abcd1234');
  		insert cty;
		
		//Create Test Vendor
		Account vAcct = new Account(Name = 'Test Vendor Account');
		insert vAcct;
		Contact v = new Contact(LastName = 'Vendor Demo', AccountId = vAcct.Id, fdicCountyId__c = 'abcd1234');
		insert v;
		
		Contact result = [SELECT Primary_County__c FROM Contact Where Id = :v.Id];      // Query Added
		
		System.assertEquals (result.Primary_County__c, cty.Id);        // Primary_County__c is a lookup
	}

 

Smiley BinduSmiley Bindu

Failure Message: "System.AssertException: Assertion Failed: Expected: null, Actual:", Failure Stack Trace: "Class.RestClientTest.testGet: line 36, column 1"

am unable to solve the above error.Its working fine in sandbox. It has no errors but in production when I validate am getting this error. Please help me in resolving this.

 

Thank You,

Bindu

sudha76sudha76

I am having a similiar problem. The code works fine on the Sandbox but on production it fails.

 

 

@isTest
private class TestClass_ForAccountTechnicalProfile
{
    static testmethod void test_Copy_AccountTechnicalProfile_OnAccount_trigger() 
    {
        Account a = new Account();
        a.Name='test - mumbai';
        a.Site_City__c ='mumbai';
        a.Type='End User';
        insert a;
        
        Account_Technical_Profile__c atp = new Account_Technical_Profile__c ();
        atp.Account__c = a.Id;
        insert atp;
        
        //Verify that the results are as expected.
        a = [Select Id,Copy_Account_Technical_Profile__c from Account where Id=:a.Id limit 1];
        System.assertEquals(a.Copy_Account_Technical_Profile__c,atp.Id);    
    }
}