System.assertEquals

What is System.assert(condition, msg)?

System.assert(condition, msg) confirms that a specified condition is true. Should the condition prove false, it triggers a fatal error, halting code execution. Here’s the signature of this method:

public static void assert(Boolean condition, Object msg)

This method entails two parameters: the condition, which is of Boolean type, and the second, is msg, which is optional and of type Object.

Ref: developer.salesforce.com

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

The method System.assertEquals asserts that the first two arguments provided are identical. If they are not, it triggers a fatal error, halting code execution. Here’s the signature of this method:

public static void assertEquals(Object expected, Object actual, Object msg)

This method features three parameters: the first being expected, of type Object, the second being actual, also of type Object, and the optional third parameter, msg, which is of type Object as well.

Ref: developer.salesforce.com

System.assertEquals Example.

@isTest
public class SampleTestClass {

@isTest
static void example(){

    List<Account> accountsToBeCreated = new List<Account>();
    //Create test data
    for(Integer i = 0; i<10; i++){
        Account newAccount = new Account();
        newAccount.name='test'+i;
        newAccount.Industry = 'Energy';

        accountsToBeCreated.add(newAccount);
    }
    //Insert test Data
    insert accountsToBeCreated;
    //Query test data that was just created
    List<Account> createdAccounts = [SELECT Id, Industry From Account];

    //Ensure 10 records were created
    System.assertEquals(10,createdAccounts.size(),'10 account records should be returned');

    //Loop through accounts ensure the industry was correctly assigned as Energy
    for(Account acc : createdAccounts){
        System.assertEquals('Energy', acc.Industry,'Industry should match');
    }

}

}

Source: levelupsalesforce.com

In the provided code snippet, AssertEquals is utilized twice. The first instance of AssertEquals verifies the successful creation of 10 records. It checks whether the variable createdAccounts contains the expected number of records. If a different count of records is detected, the error message “10 account records should be returned” will be displayed.

The subsequent AssertEquals validate that each account within the collection possesses the “Energy” industry designation. If any account has a different Industry assigned, the assertion throws an error with the message “Industry should match”.

Difference between System.assert vs System.assertEquals

While System.assertEquals and System.assert are closely related, but they exhibit some differences:

System.assert:

  • Accepts only two parameters.
  • The first parameter is mandatory, representing the condition to be tested.
  • The second parameter is optional, displaying a message if the condition is evaluated as false.

System.assertEquals:

  • Accepts three parameters.
  • The first two parameters are mandatory, representing the variables undergoing comparison.
  • The third parameter is optional, functioning as a message displayed if the comparison is false.

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

This method, System.assertNotEquals, verifies that the first two arguments provided are not identical. If they are indeed the same, a fatal error is triggered, leading to a halt in code execution. Here’s the signature of this method:

public static void assertNotEquals(Object expected, Object actual, Object msg)

This method encompasses three parameters: the first, expected, is of type Object; the second, actual, is also of type Object; and the optional third parameter, msg, is of type Object.

Please note that despite logging an exception in case of failure, an assertion failure cannot be caught using a try/catch block.

Conclusion

Mastering System.assertEquals is indispensable for crafting robust and reliable test suites. By embracing best practices and exploring diverse use cases, you empower yourself to conduct comprehensive testing, thereby elevating the quality of your software projects. Incorporate System.assertEquals judiciously into your testing workflow and reap the rewards of enhanced code integrity and accelerated development cycles.