Writing fluently readable assertions with AssertJ

The last section of this chapter will explain the fundamentals of AssertJ and explain how to improve verification readability with custom extensions.

Employing assertion chains

In Chapter 4, Testing Exceptional Flow, one of the examples uses three assertXXX statements to check whether:

  • An expected exception is not null
  • It's an instance of IllegalArgumentException
  • It provides a specific error message

The passage looks similar to the following snippet:

Throwable actual = ...

assertNotNull( actual );
assertTrue( actual instanceof IllegalArgumentException );
assertEquals( EXPECTED_ERROR_MESSAGE, actual.getMessage() );

Indeed, it takes a second or two to grasp the verification conditions. This is because there ...

Get Testing with JUnit now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.