Activity Forums Salesforce® Discussions What is the difference between assert() and assertEquals() with example in Salesforce?

  • Subhendu

    Member
    January 16, 2018 at 10:43 am

    Hi Ankit,

    System.assert take two parameters, first is the condition to check and second the msg to log if the condition is true. Whereas, System.assertEquals take three parameters first two are the values to compare and the third one to log msg.

    Hope this helps.

    Thanks,
    Subhendu

  • PRANAV

    Member
    March 28, 2018 at 9:05 am

    Hi Ankit,

    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.

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

    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”.

    Hope this helps you more.

  • Archit

    Member
    March 28, 2018 at 7:41 pm

    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.

  • Parul

    Member
    September 17, 2018 at 6:05 pm

    System.assertEquals() is used to validate two values are equal. Basically it is used in test method. This method asserts that the first two arguments, x and y are the same, if they are not a runtime exception is thrown.

    Signature of this method:
    Public static void assertEquals(Object expected, Objecet actual, object msg)

     

    Thanks

  • shariq

    Member
    September 17, 2018 at 6:16 pm

    Hi,

    To add more -

    Best Practice - 

    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

    Hope this helps.

Log In to reply.

Popular Salesforce Blogs