Salesforce Apex Testing Assertion Methods


Example

System.assert can be used to check that a boolean expression evaluates to true:

System.assert(Service.isActive());
System.assert(!Service.getItems().isEmpty(), 'items should not be empty');

System.assertEquals and System.assertNotEquals can be used to check equality of two values. The expected value is passed as the first parameter, and the value under test is passed as the second.

System.assertEquals(4, Service.getItems().size());
System.assertNotEquals(null, Service.getItems());

// failure messages are optional:
System.assertEquals(true, Service.doWork(), 'doWork should return true');
System.assertNotEquals(null, Service.doWork(), 'doWork should not be null');