System assertEquals Salesforce

System assertEquals Salesforce

System assertEquals Salesforce, Here I am posting about assert methods in System class. It helps everyone to find the differences between assert methods in salesforce.

Below are the three assert methods in system class.
1. System.assert(condition, msg)
2. System.assertEquals(expected, actual, msg)
3. System.assertNotEquals(expected, actual, msg)

What is System.assert(condition, msg)?

This method asserts that the specified condition is true. If it is not true, a fatal error returned that causes code execution to halt.

Signature of this method:
Public static void assert(Boolean condition, Object msg)

This method has two parameters. One is a condition which is a Boolean type, and the other one is msg which is optional and object type.

What is system.assertEquals(expected, actual, msg)?

Asserts that the first two arguments are the same. If they are not same, a fatal error returned that causes code execution halt.

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

This method has three parameters, one is expected which is a type of object, the second one is actual, which is also a type of an object and the other one msg which is optional and type of object.

What is system.assertNotEquals(expected, actual, msg)?

This method asserts that the first two arguments are not the same. if they are the same, a fatal error returned that causes code execution halt.

Signature of this method: public static void assertNotEquals(Object expected, Object actual, object msg)

This method has three parameters, first one expected which is a type of object, the second one is actual, which is also the type of object and the other one msg which is optional and a type of object.

Note: We cannot catch an assertion failure using a try/catch block even though it logged as an exception in case all above three methods.

To know more about this refer below Salesforce documentation URL about salesforce system class.

https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_methods_system_system.htm#apex_System_System_methods