Description | A 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: - 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. - 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);
}
|
---|