Comparison fails when converting Opportunity from/to JSON

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 =,
                                      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.


I forgot to say that using System.assert and comparing with == also fails.


I tried comparing each field separately using describe and it works. But the equals for opportunity still fails. I think this is a bug.
Here’s the code:

class OpportunityToJsonBug {
    private static testMethod void testBug() {
        Opportunity opp = new Opportunity(Name = 'Test Opp', StageName = 'Prospecting');
        String json = System.Json.serialize(opp);
        Opportunity jsonOpp = (Opportunity) System.Json.deserialize(json, Opportunity.class);
        Map<String, Schema.SObjectField> M = Schema.SObjectType.Opportunity.fields.getMap();
        for(String fieldName :  M.keySet()) {
             Object oppField  = opp.get(fieldName);
             Object jsonOppField  = jsonOpp.get(fieldName);
             System.assertEquals(oppField, jsonOppField, 'Failed when comparing field ' + fieldName);

        Opportunity controlOpp = new Opportunity(Name = 'Test Opp', StageName = 'Prospecting');
        System.assertEquals(opp, controlOpp);

        // fails here...        
        System.assertEquals(opp, jsonOpp);

Source : Link , Question Author : Juan Alvarez , Answer Author : Acuariano

Leave a Comment