September 26, 2012


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

No comments:

Post a Comment