The Silver Lining

Lessons & Learnings from a salesforce certified technical architect.

Salesforce: System.Assert vs System.AssertEquals

with 7 comments

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)

About these ads

Written by Wes

September 7, 2010 at 8:03 pm

7 Responses

Subscribe to comments with RSS.

  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.

    d3developer

    September 7, 2010 at 9:06 pm

    • Interesting, so this happens when assert variable of different types like String and Id then?

      Wes

      September 8, 2010 at 11:35 am

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

        //System.assertEquals(tlo.id, (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*/

        d3developer

        September 12, 2010 at 4:29 am

      • Actually, I double checked and it appears to be working fine now.

        d3developer

        September 12, 2010 at 4:34 am

  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 :)

    Garyb

    September 8, 2010 at 11:14 am

  3. Thanks Wes… my Internetlessness has not disappeared!

    Jeff Douglas

    September 8, 2010 at 11:18 am


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 2,199 other followers

%d bloggers like this: