Search code examples
testingassertjarchunit

ArchUnit: Prevent assertJ statements without assertion


Is it possible to create an ArchUnit rule which prevents AssertJ statements without an assertion?

For exampel: This AssertJ statement is perfectly ok, because it has both a assertThat part and an assertion.

assertThat(frodo.getName()).isEqualTo("Frodo");

I need a ArchUnit rule which will let the build fail when something like this is found:

assertThat(frodo.getName());

This is a assertThat statement without an assertion. This line of code will never throw an Assertion exception.


Solution

  • I don't think that ArchUnit can catch such statements, but some static code analysis tools can, cf. Verify that assertions have been called in Assertj.