O'Reilly logo

Testing with JUnit by Frank Appel

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required