I am facing an issue when converting an Opportunity to and then from JSON. If I put that in a test, the assertion fails when comparing both Opportunities.
Here is my code:
Test case:
static testMethod void opportunityCanBeConvertedToJSONFormatAndBack () { Opportunity opportunity = Build.anOpportunity().build(); Opportunity jsonOpportunity = Opportunities.opportunityFromJSON( Opportunities.opportunityToJSON(opportunity) ); // all this assertions pass System.assertEquals(opportunity.Name, jsonOpportunity.Name); System.assertEquals(opportunity.StageName, jsonOpportunity.StageName); System.assertEquals(opportunity.AccountId, jsonOpportunity.AccountId); System.assertEquals(opportunity.Amount, jsonOpportunity.Amount); System.assertEquals(opportunity.Sale_Steps__c, jsonOpportunity.Sale_Steps__c); System.assertEquals(opportunity.CloseDate, jsonOpportunity.CloseDate); // this assertion fails... System.assertEquals(opportunity, jsonOpportunity); }
The Build class creates the opportunity as follows:
public Opportunity build () { if (account == null) { account = anAccount().build(); insert account; } Opportunity opp = new Opportunity(Name = name, CloseDate = Date.today(), StageName = 'Opportunity stage name', AccountId = account.Id, Amount = this.anAmount, Sale_Steps__c = saleStep ); return opp; }
And finally, this is how the Opportunities class makes the JSON convertion:
public static String opportunityToJSON(Opportunity opp) { return System.Json.serialize(opp); } public static Opportunity opportunityFromJSON(String json) { return (Opportunity) System.Json.deserialize(json, Opportunity.class); }
Finally, the error message shows the same string conversion for the Opportunities,
Assertion Failed: Expected: Opportunity:{Name=An opportunity, StageName=Opportunity stage name, Amount=null, AccountId=001i000000oVNChAAO, Sale_Steps__c=Prospecting, CloseDate=2014-04-22 00:00:00}, Actual: Opportunity:{Name=An opportunity, StageName=Opportunity stage name, Amount=null, AccountId=001i000000oVNChAAO, Sale_Steps__c=Prospecting, CloseDate=2014-04-22 00:00:00}
so I am guessing that it should be a deeper comparison that is failing (maybe a TZ, or something inside the Account?), but I can't figure it out, so any help will be really appreciated.
Thanks in advance! Juan M.
Update
I forgot to say that using System.assert and comparing with == also fails.