Use AssertJ to improve your test code readability, maintenance of tests easier

AssertJ core is a Java library that provides a fluent interface for writing assertions.It provides assertions for JDK standard types and can be used with JUnit, TestNG or any other test framework.

AssertJ fluent assertions sample
Image – AssertJ fluent assertions sample

Key Features :

  • AssertJ provides a rich set of assertions, truly helpful error messages, improves test code readability and is designed to be super easy to use within your favorite IDE.
  • AssertJ gives readable error messages, soft assertions, and improved collections and exception support.
  • You can easily write assertions for your own classes, which will make your test assertions reflect the domain
  • Basic tips :
    • Java 8 assertions
    • IDE configuration to directly get assertThat in code completion
    • Describe your assertion using as(String description, Object… args)
    • Exception assertions guide
    • Using String assertions on the content of a file

    Iterable and arrays assertions :

    • Combining filtering and assertions on iterables or arrays
    • Assertions on extracted properties/fields of iterable/array elements
    • Flat(map) extracting
    • Assertions on results of a method call on iterable/array elements
  • Gather all errors message with soft assertions
  • Using a custom comparison strategy in assertions
  • Field by field comparisons
  • Using a custom representation in assertions
  • Extending assertions with conditions
  • Creating assertions for your domain
  • AssertJ provides assertions for popular libraries like Guava,Joda Time & Neo4J

Get started with one minute starting guide

If you want to convert your JUnit assertions to AssertJ,here it is!

Like this post? Don’t forget to share it!

Summary
Use AssertJ to improve your test code readability, maintenance of tests easier
Article Name
Use AssertJ to improve your test code readability, maintenance of tests easier
Description
AssertJ core is a Java library that provides a fluent interface for writing assertions.It provides assertions for JDK standard types and can be used with JUnit, TestNG or any other test framework.
Author
Publisher Name
upnxtblog
Publisher Logo

Average Rating

5 Star
0%
4 Star
0%
3 Star
0%
2 Star
0%
1 Star
0%

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

kubernetes logo Previous post Kubernetes tutorial – Create Kubernetes cluster on AWS with kops
Next post Take Meeting Notes using Otter app