O'Reilly logo

Test-Driven Java Development - Second Edition by Viktor Farcic, Alex Garcia

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

AssertJ

AssertJ works in a similar way to Hamcrest. A major difference is that AssertJ assertions can be concatenated.

To work with AssertJ, the dependency must be added to Gradle's dependencies:

testCompile 'org.assertj:assertj-core:2.0.0' 

Let's compare JUnit asserts with AssertJ:

Assert.assertEquals(5, friendships.getFriendsList("Joe").size()); 
List<String> friendsOfJoe =    Arrays.asList("Audrey", "Peter", "Michael", "Britney", "Paul");Assert.assertTrue(  friendships.getFriendsList("Joe")   .containsAll (friendsOfJoe) 
); 

The same two asserts can be concatenated to a single one in AssertJ:

assertThat(friendships.getFriendsList("Joe")) 
  .hasSize(5) 
  .containsOnly("Audrey", "Peter", "Michael", "Britney", "Paul");

This was a nice improvement. 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