Salesforce: System.Assert vs System.AssertEquals

For a time I’d wondered what the difference between the System.Assert and System.AssertEquals methods might be (System.AssertNotEquals to a lesser degree). To be honest this tip isn’t going to help much with World Peace or Curing Internetlessness, but it’s certainly gonna save you some time.

Looking at the documentation there are some obvious differences.

  • System.Assert accepts two parameters, one (mandatory) which is the condition to test for and the other a message (optional)Β to display should that condition be false.
  • System.AssertEquals and System.AssertNotEquals both accepts three parameters; the first two (mandatory) are the variables that will be tested for in/equality and the third (optional) is the message to display if the assert results in false.

Pretty standard stuff, and can all be learnt simply by Reading The Manual.

Now testing best practices state that you should assert some condition but also output a “console” message that shows the values of each of the operands. This certainly helps you debug unit tests when asserts are failing, but man I hate typing extra code just to check out some variable values! So the time saving trick that isn’t obvious is that when omitting the optional third parameter for System.AssertEquals and System.AssertNotEquals the compiler automagically outputs the values of the operands to the console log. That’s right, instead of typing:

System.assert(var1 == var2, "The value of var1 is: " +var1 + " and the value of... oh hell I don't care I mean it's just a variable");

You could type:

System.assertEquals(var1, var2);

If the second assertion fails the console will output the values of var1 and var2 with the labels “expected” and “actual”. Knowing this now I can see no exceptional reason to use System.assert any longer (sorry little guy).

As I said this trick’s not gonna start any Mexican Waves but it should help delay the onset of RSI.

(thanks to l-dawg for opening my eyes)

7 thoughts on “Salesforce: System.Assert vs System.AssertEquals”

  1. AssertEquals doesn’t always work unfortunately. I’ve gotten ‘xyz’ does not equals ‘xyz’ on Ids before, perhaps because it doesn’t automatically cast them to a comparable type.

      • I don’t remember exactly what I tried but I know I wasn’t able to get it to work correctly. Example:

        //System.assertEquals(, (Id) pcb.myObject.get(‘Test_Lookup_Object__c’));
        /*System.AssertException: Assertion Failed: Expected: a01A00000029cm2IAA, Actual: a01A00000029cm3IAA
        Class.formation.FormTestSuite.testDefaultValueSaveWithHiddenLookup: line 272, column 3
        External entry point*/

  2. If using AssertEquals, the expected result should be the first argument, the actual result should be the second argument. If you don’t pay attention, this can cause debugging problems later along the lines of “Why is that the expected value? That’s not right!”. I usually forget which way round it goes though πŸ™‚


Leave a Comment