System.ListException

How to use System AssertEquals

In order to test the apex code written in Salesforce, a developer would need to write an apex test class to cover the code.

Salesforce doesn’t allow any code to be deployed into Production until at least 75% of the code is covered overall. This is why it is important to cover maximum lines of apex code with test methods.

Another benefit of writing efficient apex test methods is to ensure that your apex logic works as expected.

How to efficiently test Apex Methods?

Salesforce offers certain built-in functions that allow a developer to quickly compare test result with the logics actual result. This ensures that the function of the apex class works as intended.

There are 3 such methods that can be used to test the logic and they belong to the System Class.

  1. System.Assert(Conditional Logic, Message):
    1. How is it defined? The System.Assert method of the System Class requires two parameters to be described. It asserts that the specified condition is true. If it is not, a fatal error is returned that causes code execution to halt.
      1. Conditional Logic: This is where you define the logic to test which must return true.
      2. Message: This is the message you print when the logic returns true.
    2. When to use it? This method is used when it is a simple true or false type statement. It can be used to test positive or negative test cases.
    3. Example:
static testMethod void testingHowSystemAssertWorks(){

    Account a = new Account();
    a.Name = "Company 1";
    a.AccountSource = "Media";
    a.NumberOfEmployees = 100;

    try{
       insert a;
       List<Account> aList = new Account aList([Select Id from Account]);
       System.assert(aList.size() == 1, '1 Account was successfully inserted');
    } Catch (DMLException e) {
       System.assert( e.getMessage().contains('Insert failed. First exception on ' +'row 0; first error:FIELD_CUSTOM_VALIDATION_EXCEPTION,'+'Mileage request exceeds daily limit(500): [Miles__c]'),e.getMessage() );
   }

2. System.AssertEquals(Expect Result, Actual Result, Message):

    1. How is it defined? The System.AssertEquals method of the System Class requires three parameters to be described. It asserts that the first two arguments are the same. If they are not, a fatal error is returned that causes code execution to halt.:
      1. Expected Result: This is the result you define must happen from running the apex class.
      2. Actual Result:  This is the result returned from running the apex class.
      3. Message: This is the message you print when the logic returns true.
    2. When to use it? This method is used to test more complex logic and returned results from the apex methods. This is primarily used to test positive test cases.
    3. Example:
static testMethod void testingHowSystemAssertEqualsWorks(){

    Account a = new Account();
    a.Name = "Company 1";
    a.AccountSource = "Media";
    a.NumberOfEmployees = 100;

    try{
       insert a;
       List<Account> aList = new Account aList([Select Id from Account]);
       System.assert(aList.size(), 1, '1 Account was successfully inserted');
    }

2. System.AssertNotEquals(Expect Result, Actual Result, Message):

    1. How is it defined? The System.AssertNotEquals method of the System Class requires three parameters to be described. It asserts that the first two arguments are different. If they are the same, a fatal error is returned that causes code execution to halt.
      1. Expected Result: This is the result you define must happen from running the apex class.
      2. Actual Result:  This is the result returned from running the apex class.
      3. Message: This is the message you print when the logic returns true.
    2. When to use it? This method is used to test more complex logic and returned results from the apex methods. This is primarily used to test negative test cases.
    3. Example:
static testMethod void testingHowSystemAssertNotEqualsWorks(){

    Account a = new Account();
    a.Name = "Company 1";
    a.AccountSource = "Media";
    a.NumberOfEmployees = 100;

    try{
       insert a;
       List<Account> aList = new Account aList([Select Id from Account]);
       System.assert(aList.size(), 0, 'Account List should not be empty');
    }

In Conclusion

It is very useful to use assert methods in test classes as they can prevent apex code from being deployed that is going to break in Production. It is deemed best practice to first write apex test classes before writing the actual apex method. This is called Test Driven Development.