System.assertEquals

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

Sample Code:


Trigger:


trigger AccountTrigger on Account ( before insert ) {


    for ( Account acct : trigger.new ) {
        
        if ( acct.Name == ‘Test Account’ )
            acct.Description = ‘Test’;
    }
    
}


Test Class:


@isTest
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().

Leave a Reply