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
halopezhalopez 

Asserting equality in custom classes.

Hi all,

 

I have the following error:

 

System.AssertException: Assertion Failed: Expected: ProductModelController:[contents=12345, creator=cchap@testconfigit.com, name=AltivarSample.configuration, referencePM=SEP_ATVCBL_V14_CTO.vt, visibility=true], Actual: ProductModelController:[contents=12345, creator=cchap@testconfigit.com, name=AltivarSample.configuration, referencePM=SEP_ATVCBL_V14_CTO.vt, visibility=true]

 

What I am doing in the test is simply creating two objects and comparing them. The code coverage hits the equals() method in the class, but when I am trying to assert whether two custom objects are the same, apex fails.

 

Is it just me, or that should be an error in the Apex compiler?

 

Cheers,

 

HA

sfdcfoxsfdcfox

I concur that it does not operate as expected:

 

UserObj a = new UserObj(5), b = new UserObj(5);
System.assert(a==b);       // Okay!
System.assertEquals(a,b);  // Error (?!)

It appears that System.assertEquals ignores Object.equals and Object.hashCode (the two functions required to use custom objects as keys in sets/maps). You'll have to use the former method, not the latter, for now.