May 17, 2012

Example for System.assertEquals()

Sample Code:


trigger AccountTrigger on Account ( before insert ) {

    for ( Account acct : ) {
        if ( acct.Name == 'Test Account' )
            acct.Description = 'Test';

Test Class:

private class SampleTestClass {

    static testMethod void insertAcctTest() {

        Account acc = new Account(Name = 'Test Account');
        insert acc;
        acc = [ SELECT Description FROM Account WHERE Id = : acc.Id ];
        System.assertEquals('Test', acc.Description);



In the above example, the expected Description of the Account is 'Test'. If any other additional processes like Workflow Field update, Process Builder, etc updates the Description other than 'Test', it will throw an error and fails the test method. This will make sure that developed code is working as expected.

Opposite to System.assertEquals() is System.assertNotEquals().


  1. For picklist field comparison in System.assertEquals statement is returning false. Any idea how to resolve this problem?

    1. Check whether any workflow or process builder or trigger is changing the field value.