Apex BOOST Library
v1.7 from Lucidware Solutions (LWS)

Assert

global inherited sharing class Assert
DescriptionA suite of assertion methods to be used within your Apex code, not in unit tests. Such assertions are commonly used to validate assumptions such as input parameters, object state, or return values. Note that assertions should never be used for handling expected errors. Instead, use Assert to enforce and document code design restrictions.

Apex BOOST assertions have two primary advantages over System.assert methods:

  1. They can be caught by custom code. Unlike System.AssertException, aBoost.AssertException is a custom exception and can therefore be caught. This makes it possible for you to use aBoost.Assert within your code, and write unit tests that test those assertions.
  2. They can be disabled with the Debug Level Apex BOOST custom settings, allowing you to utilize assertions during development and debugging, but not incur the overhead in production.
Usage:
 // validate a parameter using an assertion
 public with sharing class MyClass {
   public MyClass(Id imporantId) {
     aBoost.Assert.areNotEqual(null, importantId, 'an importantId is required!');
     ...
   }
 }

 // test parameter validation on the MyClass constructor
 @IsTest class MyClassTest {
   @IsTest static void testConstructor() {
     Test.startTest();
       try {
         new MyClass(null);
         System.assert(false, 'a missing importantId should fail');
       } catch (aBoost.AssertException expected) {}
     Test.stopTest();
   }
 }
 
For larger or more expensive validation code blocks, consider using Assert.isEnabled:
 // make sure accounts have phone numbers
 if (aBoost.Assert.isEnabled()) {
   Account[] badAccounts = [SELECT Id FROM Account WHERE Phone = null];
   aBoost.Assert.areEqual(0, badAccounts.size(), 'bad accounts: ' + badAccounts);
 }
 

Methods

  areEqual

global static void areEqual(Object v1, Object v2, Object message)
Description Assert two objects are equal, similar to System.assertEquals, but with two key differences:
  • aboost assertions can be disabled with the Debug Level in Apex BOOST custom settings
  • aboost assertions can be caught, making them ideal for validating assumptions such as input parameters, object state, or return values.
Parametermessage: diagnostic information (text, exception, etc.)
ThrowsAssertException

  areEqual

global static void areEqual(Object v1, Object v2)
Description Assert two objects are equal, similar to System.assertEquals, but with two key differences:
  • aboost assertions can be disabled with the Debug Level in Apex BOOST custom settings
  • aboost assertions can be caught, making them ideal for validating assumptions such as input parameters, object state, or return values.
ThrowsAssertException

  areNotEqual

global static void areNotEqual(Object v1, Object v2, Object message)
Description Assert two objects are not equal, similar to System.assertNotEquals, but with two key differences:
  • aboost assertions can be disabled with the Debug Level in Apex BOOST custom settings
  • aboost assertions can be caught, making them ideal for validating assumptions such as input parameters, object state, or return values.
Parametermessage: diagnostic information (text, exception, etc.)
ThrowsAssertException

  areNotEqual

global static void areNotEqual(Object v1, Object v2)
Description Assert two objects are not equal, similar to System.assertNotEquals, but with two key differences:
  • aboost assertions can be disabled with the Debug Level in Apex BOOST custom settings
  • aboost assertions can be caught, making them ideal for validating assumptions such as input parameters, object state, or return values.
ThrowsAssertException

  isEnabled

global static Boolean isEnabled()
Returnstrue if assertions are enabled (Apex BOOST custom settings Debug Level is DEBUG or less)

  isFalse

global static void isFalse(Boolean condition, Object message)
Description Assert a condition is false, similar to System.assert, but with two key differences:
  • aboost assertions can be disabled with the Debug Level in Apex BOOST custom settings
  • aboost assertions can be caught, making them ideal for validating assumptions such as input parameters, object state, or return values.
Parametermessage: diagnostic information (text, exception, etc.)
ThrowsAssertException

  isFalse

global static void isFalse(Boolean condition)
Description Assert a condition is false, similar to System.assert, but with two key differences:
  • aboost assertions can be disabled with the Debug Level in Apex BOOST custom settings
  • aboost assertions can be caught, making them ideal for validating assumptions such as input parameters, object state, or return values.
ThrowsAssertException

  isTrue

global static void isTrue(Boolean condition, Object message)
Description Assert a condition is true, similar to System.assert, but with two key differences:
  • aboost assertions can be disabled with the Debug Level in Apex BOOST custom settings
  • aboost assertions can be caught, making them ideal for validating assumptions such as input parameters, object state, or return values.
Parametermessage: diagnostic information (text, exception, etc.)
ThrowsAssertException

  isTrue

global static void isTrue(Boolean condition)
Description Assert a condition is true, similar to System.assert, but with two key differences:
  • aboost assertions can be disabled with the Debug Level in Apex BOOST custom settings
  • aboost assertions can be caught, making them ideal for validating assumptions such as input parameters, object state, or return values.
ThrowsAssertException