S2699: Add approve to assertion method name pattern

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.

You might want to tag the language. I assume Java.

1 Like

I think I did. Hope I used the right tags.

1 Like

Hi @mkutz,

thanks a lot for your report! We created a Jira ticket to fix this FP.

1 Like