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 is a lot of redundant clutter: the relevant attributes of the Throwable
type are checked one by one, always repeating the "assert" prefix and dispatching the actual
parameter to the assertion statements. AssertJ, [ASSERJ], strives to improve...