The S2699 rule recognizes method names matching (assert|verify|fail|should|check|expect|validate|andExpect).* as assertions. I’d like to propose adding approve to this pattern.
Approval testing (also known as snapshot testing) is a well-established testing pattern where a test’s output is compared against a previously approved “golden master” value. If they differ, an AssertionError is thrown — exactly like a traditional assertion.
The word approve is unambiguous in a test context and clearly signals verification intent. Adding it would eliminate false positives for approval testing libraries (e.g. ApproveJ, ApprovalTests.Java) without requiring each user to configure customAssertionMethods.
Example false positive:
@Test
void product_json() {
approve(product).printedAs(json()).byFile(); // throws AssertionError on mismatch
}
Sonar reports: “Add at least one assertion to this test case.”
The proposed change in UnitTestUtils.java would be minimal:
- "(assert|verify|fail|should|check|expect|validate|andExpect).*"
+ "(assert|verify|fail|should|check|expect|validate|andExpect|approve).*"
Users can work around this today by configuring the customAssertionMethods rule parameter, but this requires every project using approval testing to configure it individually.