assertj断言异常_编写自定义的AssertJ断言
生活随笔
收集整理的這篇文章主要介紹了
assertj断言异常_编写自定义的AssertJ断言
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
assertj斷言異常
AssertJ是廣泛使用的Hamcrest匹配器的替代匹配庫。 實際上,對于我自己的項目,我已經更改為僅使用AssertJ-我只是發現流暢的界面和可擴展性非常吸引人。
您可以編寫自定義斷言,如下所示:
想象一下一種具有強度和飲料類型的咖啡 ,例如Espresso或Latte 。 定制CoffeeAssert根據其自定義業務邏輯(在本例中為屬性)驗證咖啡實例。
public class CoffeeAssert extends AbstractAssert<CoffeeAssert, Coffee> {public CoffeeAssert(Coffee actual) {super(actual, CoffeeAssert.class);}public static CoffeeAssert assertThat(Coffee actual) {return new CoffeeAssert(actual);}public CoffeeAssert hasType(Coffee.Type type) {isNotNull();if (actual.getType() != type) {failWithMessage("Expected the coffee type to be <%s> but was <%s>", type, actual.getType());}return this;}// hasStrength(Strength) omitted ...public CoffeeAssert isNotDecaf() {isNotNull();if (actual.getStrength() == Coffee.Strength.DECAF) {failWithMessage("Expected a coffee but got decaf!");}return this;} }然后可以使用自定義斷言簡單地驗證咖啡實例。 assertThat的靜態導入必須引用CoffeeAssert 。
import static com.example.coffee.CoffeeAssert.assertThat; ...Coffee coffee = new Coffee(); coffee.setStrength(Strength.STRONG); coffee.setType(Type.ESPRESSO);assertThat(coffee).hasType(Type.ESPRESSO).isNotDecaf();使用自定義斷言可以極大地提高測試代碼的質量。
這篇帖子從我的時事通訊012中轉貼了
發現帖子有用嗎? 訂閱我的時事通訊,獲取有關IT和Java的更多免費內容,技巧和竅門:
翻譯自: https://www.javacodegeeks.com/2018/01/write-custom-assertj-assertions.html
assertj斷言異常
總結
以上是生活随笔為你收集整理的assertj断言异常_编写自定义的AssertJ断言的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 中国信创电脑系统(信创电脑操作系统)
- 下一篇: 漂流瓶子电脑版下载(漂流瓶pc下载)