I want to report a bug against Sonar rule java:S5961 - Test methods should not contain too many assertions.
In its counting of assertions within a test method, it includes the AssertJ description method .as() , its alias .describedAs(), .withFailMessage() and .overridingErrorMessage() as assertions. This is incorrect: these methods only set a description or custom error message for the assertion, but do not assert anything.
In the screenshot, you can see that there are 13 assertions, but the rule counts 26 of them.
Software used
- SonarQube Enterprise Edition v2025.5 (113872)
- IntelliJ IDEA 2024.3.2.2 (Ultimate Edition)
- SonarQube for IDE 11.5.0.82678
Steps to reproduce
Here is a code sample that will trigger a false-positive for rule java:S5961. (This sample will also trigger rule java:S5853, which is correct.)
import org.junit.jupiter.api.Test;
import static org.assertj.core.api.Assertions.assertThat;
class MyObjectTest {
@Test
void testForS5961() {
var myObject = new Object();
assertThat(myObject).as("description").isNotNull();
assertThat(myObject).as("description").isNotNull();
assertThat(myObject).as("description").isNotNull();
assertThat(myObject).as("description").isNotNull();
assertThat(myObject).as("description").isNotNull();
assertThat(myObject).as("description").isNotNull();
assertThat(myObject).as("description").isNotNull();
assertThat(myObject).as("description").isNotNull();
assertThat(myObject).as("description").isNotNull();
assertThat(myObject).as("description").isNotNull();
assertThat(myObject).describedAs("hjksa").isNotNull();
assertThat(myObject).withFailMessage("hjksa").isNotNull();
assertThat(myObject).overridingErrorMessage("hjksa").isNotNull();
}
}
