Home » Java » Core Java » AssertJ > Fest > Hamcrest

About Shaun Abram

Shaun Abram

AssertJ > Fest > Hamcrest

I have previously blogged about Hamcrest, and using its assertThat methods in preference to JUnit’s Assert.

However, I quickly after discovered FEST Assertions, and happily switched to it. It provides the same improved test readability and improves failure messages as Hamcrest, but has the extra benefit of enabling IDE auto completion, rather than having to search through package and class docs to find the right matcher.

Unfortunately, Fest seems to not longer be actively developed. The last stable release of the 1.x branch, 1.4, was released way back in 2011, and the new 2.x branch never made it to a stable release and hasn’t had a commit since June 2013.

Enter AssertJ

Assert J

AssertJ is a fork of Fest Assert, and seems to provide all the benefits plus a bunch of new features.


For example, it has all the nifty collections handling I so liked from Fest:

List<String> stringList = Lists.newArrayList("A", "B", "C");
 assertThat(stringList).contains("A"); //true
 assertThat(stringList).doesNotContain("D"); //true
 assertThat(stringList).containsOnly("A"); //false
 assertThat(stringList).containsExactly("A", "C", "B"); //false
 assertThat(stringList).containsExactly("A", "B", "C"); //true

Collect all errors before failing

It also has the ability to catch all failures before failing. For example, the above eample would fail as the first failed assumpion. The below example allows you to see all failed assertions:

List<String> stringList = Lists.newArrayList("A", "B", "C");
 SoftAssertions softly = new SoftAssertions();
 softly.assertThat(stringList).contains("A"); //true
 softly.assertThat(stringList).containsOnly("A"); //false
 softly.assertThat(stringList).containsExactly("A", "C", "B"); //false
 softly.assertThat(stringList).containsExactly("A", "B", "C"); //true
 // Don't forget to call SoftAssertions global verification!

And results in a message like this:

The following 2 assertions failed:
 <["A", "B", "C"]>
to contain only:
elements not found:
and elements not expected:
 <["B", "C"]>
Actual and expected have the same elements but not in the same order, at index 1 actual element was:
whereas expected element was:

Definitely worth a look. AssertJ core code and issue tracker are hosted on github.

Reference: AssertJ > Fest > Hamcrest from our JCG partner Shaun Abram at the Shaun Abram’s blog blog.
(0 rating, 0 votes)
You need to be a registered member to rate this.
Start the discussion Views Tweet it!
Do you want to know how to develop your skillset to become a Java Rockstar?
Subscribe to our newsletter to start Rocking right now!
To get you started we give you our best selling eBooks for FREE!
1. JPA Mini Book
2. JVM Troubleshooting Guide
3. JUnit Tutorial for Unit Testing
4. Java Annotations Tutorial
5. Java Interview Questions
6. Spring Interview Questions
7. Android UI Design
and many more ....
I agree to the Terms and Privacy Policy

Leave a Reply


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

Notify of