Assertion without a message
ID |
apex.correctness.assertions_message |
Severity |
low |
Remediation Complexity |
trivial |
Remediation Risk |
low |
Remediation Effort |
low |
Resource |
Test Quality |
Language |
Apex |
Description
Reports a BOOLEAN assertion - System.assert, Assert.isTrue, Assert.isFalse, Assert.isNull, Assert.isNotNull - called without its message argument. The comparison forms are deliberately not reported: when assertEquals fails the platform already prints the expected and the actual value, so the failure is diagnosable without a message.
Rationale
A failing assertion without a message reports only the values and a line number, which is exactly what a deployment log strips down to. The reader gets "Assertion Failed: Expected: null, Actual: 0" and has to open the test to learn what was being checked. A one-line message naming the behaviour under test turns the failure into a description of what broke - and it is the only documentation that survives into the pipeline output.
The following code illustrates the pattern detected by this rule:
@IsTest
private class AssertMessagesTest {
@IsTest
static void createsRenewal() {
List<Account> accts = new List<Account>{ new Account(Name = 'Acme') };
insert accts;
// FLAGGED: Assertion without a message
System.assert(accts[0].Id != null);
// FLAGGED: Assertion without a message
Assert.isNotNull(accts[0].Id);
// FLAGGED: Assertion without a message
Assert.isTrue(accts[0].Name.startsWith('A'));
System.assert(accts[0].Id != null, 'the insert assigns an id');
Assert.isNotNull(accts[0].Id, 'the insert assigns an id');
// A failing comparison already prints the expected and the actual value, so these
// are not reported even without a message.
System.assertEquals(1, accts.size());
Assert.areEqual('Acme', accts[0].Name);
}
}