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

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.

主站蜘蛛池模板: 桐乡市| 玛多县| 禹城市| 枣庄市| 夏河县| 姜堰市| 麟游县| 双江| 金寨县| 高州市| 九龙城区| 松江区| 永新县| 武陟县| 赤壁市| 陈巴尔虎旗| 宁陕县| 霞浦县| 许昌市| 广丰县| 隆化县| 鄂温| 平定县| 周至县| 砀山县| 呼和浩特市| 静乐县| 广西| 富宁县| 桑植县| 太白县| 枝江市| 秀山| 华阴市| 平舆县| 肃北| 辉县市| 岗巴县| 土默特左旗| 蒲江县| 宝应县|