Assert test results in Apex | Assert Class

Telegram logo Join our Telegram Channel

Say goodbye to the System.assert--- methods. The new Assert class provides you with all the functions you need to check conditions for the Apex tests. Also other than the assert functions there is more to it. The Assert class provides all the basic functions similar to the System class but there are a few advanced functions that are new. Let us first understand how to use the basic functions.


The basic Assert class methods.

Check for equality - Assert.areEqual()

These methods allow you to check if the two values are equal or not. The first argument is the expected value and the second argument is the output of the method that you are testing. The assertion is failed if both of the values are not equal.

String sub = 'abcde'.substring(2);
Assert.areEqual('cde', sub); // Succeeds

// assert equality with a message when assertion is failed.
String sub = 'abcde'.substring(2);
Assert.areEqual('ab', sub, 'Expected characters after first two'); // gives an error

Check for non-equality - Assert.areNotEqual()

These methods allow you to check if the two arguments are not equal, the first one is the expected value and the second is the output of the method that you are testing. The assertion is failed if both of the values are equal.

String sub = 'abcde'.substring(2);
Assert.areNotEqual('xyz', sub); // Succeeds

// assert non equality with a message when assertion is failed.
String sub = 'abcde'.substring(2);
Assert.areNotEqual('xyz', sub, 'Characters not expected after first two'); // Succeeds

Check for false condition - Assert.isFalse()

With this method you can test the methods that are returning boolean values and when you expect the true result. Also, you can directly pass down a boolean expression as an argument to this function. The assertion is failed when a true value is passed.

Boolean containsCode = 'Salesforce'.contains('code');
Assert.isFalse(containsCode); // Assertion succeeds

// assert for false with a message when assertion is failed.
Boolean containsCode = 'Salesforce'.contains('code');
Assert.isFalse(containsCode, 'No code'); // Assertion succeeds

Check for true condition - Assert.isTrue()

With this method you can test the methods that are returning boolean values and when you expect the false result. Also, you can directly pass down a boolean expression as an argument to this function.
The assertion is failed when a false value is passed.

String myString = null; 
Assert.isNull(myString); // Succeeds

// assert for true condition with a message when assertion is failed.
Boolean containsForce = 'Salesforce'.contains('force');
Assert.isTrue(containsForce, 'Contains force'); // Assertion succeeds

Check if an argument is null - Assert.isNull()

If the method that you are testing is expected to return a null value, then you can use this function. If the value is not null then assertion is failed.

String myString = null; 
Assert.isNull(myString); // Succeeds

// assert for null condition with a message when assertion is failed.
String myString = null; 
Assert.isNull(myString, 'String should be null'); // Succeeds

Check if an argument is not null - Assert.isNotNull()

Use these functions when you are expecting any non-null values from the tested method. If the value is null it will fail the assertion.

String myString = 'value'; 
Assert.isNotNull(myString); // Succeeds

// assert for not-null condition with a message when assertion is failed.
String myString = 'value'; 
Assert.isNotNull(myString, 'myString should not be null'); // Succeeds


Advanced Assert class methods

Check if the returned value is an instance of the specific object: Assert.isInstanceOfType()

When you are working on methods that can return multiple different types of objects then sometimes you need to know that the 'tested' method is returning the right type based on the argument.

For example, if there is a method that returns the generic SObject record but you want to check if the returned record is of type Account or Contact, then you can use the isInstanceOfType method to assert that and make your tests foolproof. 

In the below code, the function searchLeadOrContact might return an instance of a lead or a contact. 

public class SearchInLeadOrContact {
    public static SObject searchLeadOrContact(String emailAddress){
        List<List<SObject>> results = [
            FIND :emailAddress
            IN EMAIL FIELDS 
            RETURNING Contact(Id LIMIT 1),Lead(Id LIMIT 1)
        ];
        
        // if contact is present then no need to check for lead, just return the contact.
        if(results[0].size() == 1){
            return results[0][0];
        } else if(results[1].size() == 1){
            return results[1][0];
        }
        return null;
    }
}

See the below test class which uses the instance of method to verify that the returned value is a lead or contact.


Intentionally fail apex test method using Assert.fail()


No comments :
Post a Comment

Hi there, comments on this site are moderated, you might need to wait until your comment is published. Spam and promotions will be deleted. Sorry for the inconvenience but we have moderated the comments for the safety of this website users. If you have any concern, or if you are not able to comment for some reason, email us at rahul@forcetrails.com