Test your code not throwing any exception

with JUnit and Assertj

Recently I had to test that the code does not throw any exception. We all know that AssertJ gives us a powerful set of assertions which improves your test code readability.

And again, AssertJ helped me to do the job with assertThatCode:

    Service service = new Service();
    assertThatCode(
        () -> {
          service.method();
        })
    .doesNotThrowAnyException();

You can find more assertions example on github.

I hope you learned a little something 🙂

Links

Github project https://github.com/claudioaltamura/java-assertj

AssertJ https://assertj.github.io/doc/