Insufficient number of assertions
Why is this an issue?
Test methods without assertions only verify that code executes without throwing exceptions. They do not validate that the code produces correct results. Such tests provide false confidence and may miss bugs that cause incorrect behavior without errors.
Examples
Example of incorrect code:
@IsTest
static void testCreateAccount() {
AccountService.createAccount('Test Account');
// No assertions - only verifies no exception was thrown
}
Example of correct code:
@IsTest
static void testCreateAccount() {
Test.startTest();
Account result = AccountService.createAccount('Test Account');
Test.stopTest();
Assert.isNotNull(result.Id, 'Account should be inserted');
Assert.areEqual('Test Account', result.Name, 'Account name should match');
List<Account> queriedAccounts = [SELECT Id FROM Account WHERE Name = 'Test Account'];
Assert.areEqual(1, queriedAccounts.size(), 'One account should exist in the database');
}
@IsTest
private class AccountServiceTest {
@IsTest
static void shouldRegisterNewAccountAndCommit() {
// Arrange β create the mock
fflib_ApexMocks mocks = new fflib_ApexMocks();
IAccountsUnitOfWork mockUow = (IAccountsUnitOfWork) mocks.mock(IAccountsUnitOfWork.class);
// Act β call the real service, injecting the mock
AccountService service = new AccountService(mockUow);
service.createAccount('Acme Corp');
// Assert β verify the mock received the expected calls
((IAccountsUnitOfWork) mocks.verify(mockUow))
.registerNew(
fflib_Match.sObjectWith(
new Map<SObjectField, Object>{
Account.Name => 'Acme Corp'
}
)
);
((IAccountsUnitOfWork) mocks.verify(mockUow))
.commitWork();
}
}
How can I fix violations?
Add meaningful assertions to your test methods:
Assert return values: Verify that methods return expected values.
Assert database state: Query records to verify they were created, updated, or deleted correctly.
Assert field values: Check that specific fields contain expected values.
Assert collection sizes: Verify that lists and sets contain the expected number of items.
Verify invocations: Verify that a mocked object's method is called.
When should I disable this rule?
You may dismiss specific violations for test methods that intentionally verify exception handling, where the assertion is implicit in the try-catch structure.
Alternatively, you can configure this rule to require a different minimum number of assertions per test method.
Resources
