Example for System.assertEquals()

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

2 comments:

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

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

      Delete