Assert list size
When testing collection sizes with JUnit Jupiter, it's easy to make mistakes with argument order or write verbose assertions.
Type-specific assertions
AssertJ provides rich, expressive assertions tailored to specific types. These specialized assertions make your tests more readable and provide better failure messages compared to generic boolean checks.
Chained and combined assertions
AssertJ's fluent API lets you chain multiple assertions on the same object, and combine checks for multiple elements into a single statement. This makes tests more concise, more readable, and produces better failure messages.
Precise assertions
While contains() verifies that specific elements are present in a collection, it doesn't ensure that only those elements exist.
Migrate to AssertJ
You can easily convert existing assertions to use AssertJ using OpenRewrite's AssertJ recipe.
Encourage usage
After you've migrated to AssertJ once, you'll want to ensure that all new tests also use AssertJ.
Return actual object
When writing helper methods that perform assertions and return the asserted value, AssertJ provides a convenient way to combine both operations.
Practice: Improve Test Expressiveness
Ready to practice improving test expressiveness with AssertJ? We've prepared a test class with verbose JUnit assertions that you can refactor.