Search code examples
javasonarqubeassertj

Verify that assertions have been called in Assertj


I'm reading through test classes that use Assertj to verify results. Occasionally, I've spotted an assertThat without assertions.

assertThat(object.getField());

Is it possible to identify these classes somewhere in the development cycle? My first guess would be to use a custom Sonar rule. Although I don't see how I should define that this method should be followed by an assertion (a method returning void?).


Solution

  • SonarJava is having the rule S2970 "Assertions should be complete" that can detect assertThat without assertions for AssertJ, Fest and Truth.

    See: https://rules.sonarsource.com/java/RSPEC-2970