1

When using the AssertJ library, I'm interested in being able to assert that all org.assertj.core.api.Assert objects had an actual assertion made on them. That is, a test such as the following should fail because it is clearly a mistake:

import static org.assertj.core.api.Assertions.assertThat;

// ... elided ...

@Test
public void unfinishedAssertion() {
    assertThat("str");
}

Is there a way to configure this with AssertJ, or to make this assertion in a JUnit @After method?

My only thought is to provide a static factory like assertThat that returns a proxy, delegating all method invocations to the underlying Assert, and using an After method to assert that the proxy had at least one invocation with a method that was not Assert#as or Assert#withFailMessage, etc., but this seems cumbersome and unnecessary as the library should provide this functionality itself.

Ivan G.
  • 700
  • 8
  • 19
  • Mockito, for example, does a great job of reducing potential mistakes by throwing an exception when a stubbing is unfinished: `Mockito.when(obj.toString());` will fail a test – Ivan G. May 30 '19 at 15:11

1 Answers1

0

At the moment, the best way is to use findbugs/spotbugs to warn you about this kind of misuse, see https://assertj.github.io/doc/#assertj-core-incorrect-usage for details.

Hope it helps.

Joel Costigliola
  • 6,308
  • 27
  • 35
  • This is a helpful link, static analysis was also the recommendation by a colleague. Does AssertJ have plans to support this as a situation that throws an exception though? – Ivan G. May 31 '19 at 14:10
  • I'm not sure there is an easy way to do that in the library but I'm happy to be proven wrong. – Joel Costigliola Jun 01 '19 at 03:11
  • this should help too https://stackoverflow.com/questions/49406872/verify-that-assertions-have-been-called-in-assertj?rq=1 – Joel Costigliola Jun 01 '19 at 03:13