官术网_书友最值得收藏!

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 was no need to have two separate asserts, nor was there a need to create a new list with expected values. Moreover, AssertJ is more readable and easier to understand.

The complete source code can be found in the FriendshipsAssertJTest class at https://bitbucket.org/vfarcic/tdd-java-ch02-example-junit.

Now that we have the tests up and running, we might want to see what the code coverage is that is generated by our tests.

主站蜘蛛池模板: 岗巴县| 合江县| 商丘市| 蒙阴县| 镇远县| 商丘市| 昭觉县| 墨玉县| 台州市| 荣成市| 射洪县| 海伦市| 刚察县| 丹棱县| 文昌市| 剑河县| 航空| 鄢陵县| 六枝特区| 永平县| 旬阳县| 尼玛县| 西昌市| 普格县| 蒙阴县| 农安县| 新宁县| 高邑县| 双柏县| 忻城县| 丹寨县| 景泰县| 民丰县| 皮山县| 镇坪县| 永平县| 专栏| 贵港市| 贡嘎县| 抚宁县| 贵德县|