- Test-Driven Java Development(Second Edition)
- Alex Garcia Viktor Farcic
- 149字
- 2021-06-24 18:31:48
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.
- iOS Game Programming Cookbook
- What's New in TensorFlow 2.0
- Java程序設(shè)計(jì)與計(jì)算思維
- Functional Programming in JavaScript
- 游戲程序設(shè)計(jì)教程
- CKA/CKAD應(yīng)試教程:從Docker到Kubernetes完全攻略
- JavaScript by Example
- Mastering Google App Engine
- Apache Kafka Quick Start Guide
- Python極簡(jiǎn)講義:一本書入門數(shù)據(jù)分析與機(jī)器學(xué)習(xí)
- Microsoft Azure Storage Essentials
- Mastering Git
- Learning Continuous Integration with TeamCity
- App Inventor創(chuàng)意趣味編程進(jìn)階
- Go語(yǔ)言開發(fā)實(shí)戰(zhàn)(慕課版)