v6.2.0
v6.2.0 - 18-October-2024
Scope
- Enhanced Mocking Capabilities
- Documentation Update
SOQL
- Added support for mocking query exceptions
- Improved error testing capabilities
Mocking Query Exceptions
SOQL Lib now supports mocking query exceptions, enabling you to test error handling scenarios in your Apex code. This is essential for validating that your application gracefully handles database errors, security violations, and other query-related exceptions.
Default Exception Mock
Use .throwException() to simulate a standard query exception with the default message: "List has no rows for assignment to SObject".
Controller
public with sharing class ExampleController {
public static Account getAccountById(Id accountId) {
try {
return (Account) SOQL.of(Account.SObjectType)
.with(Account.Name, Account.BillingCity)
.byId(accountId)
.mockId('ExampleController.getAccountById')
.toObject();
} catch (Exception e) {
// Logger here
throw e;
}
}
}Test Class
@IsTest
static void getAccountByIdException() {
SOQL.mock('ExampleController.getAccountById').throwException();
Test.startTest();
Exception error;
try {
Account result = ExampleController.getAccountById('001000000000000AAA');
} catch (Exception e) {
error = e;
}
Test.stopTest();
Assert.isNotNull(error, 'The query exception should be thrown.');
}Custom Exception Message Mock
Use .throwException(message) to simulate a query exception with a custom error message, such as field-level security errors or invalid field references.
Test Class
@IsTest
static void getAccountByIdException() {
String errorMessage = 'No such column \'InvalidField__c\' on entity \'Account\'.';
SOQL.mock('ExampleController.getAccountById').throwException(errorMessage);
Test.startTest();
Exception error;
try {
Account result = ExampleController.getAccountById('001000000000000AAA');
} catch (Exception e) {
error = e;
}
Test.stopTest();
Assert.isNotNull(error, 'The query exception should be thrown.');
Assert.isTrue(error.getMessage().contains('InvalidField__c'));
}Documentation Update
Added comprehensive documentation for exception mocking, including:
- Detailed examples of both default and custom exception messages
- Best practices for testing error handling scenarios
- Integration examples with controller error handling patterns
🚨 Breaking Changes 🚨
There are no breaking changes in this release. All new features are additive.