Java: proper use of assertSoftly

SoftAssertions.assertSoftly(..) allows multiple assertions at once. It is similar to JUnit5 assertAll(executables).
It requires that assertThat(..) from softly is called otherwise only the first failing assertion terminates the test and the usage assertSoftly is pointless.

Given: Person person = new Person("John", 25);

Noncompliant Code

SoftAssertions.assertSoftly(softly -> {
    assertThat(person.name()).isEqualTo("Paul");
    assertThat(person.age()).isEqualTo(-1);
});

Compilant Code

SoftAssertions.assertSoftly(softly -> {
    softly.assertThat(person.name()).isEqualTo("Paul");
    softly.assertThat(person.age()).isEqualTo(-1);
});
1 Like

Thank you, Björn, for the great suggestion! We’ve created a rule proposal, SONARJAVA-5729, and will be discussing it further and planning for implementation. We appreciate your contribution!