assertj_AssertJ的SoftAssertions –我们需要它们吗?
assertj
編寫好的單元測試的規則之一是,它應該由于一種原因而失敗,因此,單元測試應該測試一種邏輯概念。 有時很難在每個測試中擁有一個斷言。 為了遵循規則,我們可能在單個測試中每個對象具有多個斷言。
但是,在一個測試中存在多個斷言的問題在于,如果第一個斷言由于任何原因而失敗,我們實際上將不知道其他斷言,因為它們將不會被執行。 并且您知道了演練:您檢查斷言失敗原因,進行修復,然后重新運行測試。 也許您很幸運,測試會通過。 但是也許它將因另一個斷言而失敗。 對于真正快速的單元測試,這不是什么大問題,但是例如,在進行Selenium測試時,分析和故障檢測可能會變得很麻煩并且肯定會很費時。
幸運的是,借助AssertJ的SoftAssertions ,我們可以重新考慮在測試中創建斷言的SoftAssertions 。
一個宣稱可以統治所有人的主張!
在假設的Dice游戲中,有一個Score對象,其中保存得分值,骰子組合和提醒。 在單元測試中,我們可能想驗證不同骰子組合的分數是如何計算的。
在下面的示例中,驗證了一個概念(分數對象):
@Test public void verifiesScore() {Score score = Score.scoreBuilder().withValue(11).withCombination(dice(1, 1, 3, 4)).withReminder(dice(6)).build();assertThat(score.getValue()).as("Has score").isEqualTo(8);assertThat(score.getCombination()).as("Has combination").isEqualTo(dice(1, 1, 3, 3));assertThat(score.getReminder()).as("Has reminder").isEqualTo(dice(5)); }如您所見,所有三個斷言都失敗了,但是由于第一個失敗后測試的執行停止,因此我們只會看到第一個失敗的結果:
org.junit.ComparisonFailure: [Has score] Expected :8 Actual :11引入
為了解決這個問題,我們可以使用SoftAssertions ,它將在調用assertAll()方法時立即收集所有斷言的結果:
@Test public void verifiesScoreSoftly() {Score score = Score.scoreBuilder().withValue(11).withCombination(dice(1, 1, 3, 4)).withReminder(dice(6)).build();SoftAssertions softAssertions = new SoftAssertions();softAssertions.assertThat(score.getValue()).as("Has score").isEqualTo(8);softAssertions.assertThat(score.getCombination()).as("Has combination").isEqualTo(dice(1, 1, 3, 3));softAssertions.assertThat(score.getReminder()).as("Has reminder").isEqualTo(dice(5));softAssertions.assertAll(); }現在我們可以驗證測試中的所有斷言失敗:
org.assertj.core.api.SoftAssertionError: The following 3 assertions failed: 1) [Has score] expected:<[8]> but was:<[11]> 2) [Has combination] expected:<...alue=3}, Dice{value=[3]}]> but was:<...alue=3}, Dice{value=[4]}]> 3) [Has reminder] expected:<[Dice{value=[5]}]> but was:<[Dice{value=[6]}]>JUnitSoftAssertions
代替手動創建SoftAssertions并調用其assertAll()我們可以使用JUnit @Rule :
@Rule public JUnitSoftAssertions softAssertions = new JUnitSoftAssertions();@Test public void verifiesScoreSoftlyUsingRule() {Score score = Score.scoreBuilder().withValue(11).withCombination(dice(1, 1, 3, 4)).withReminder(dice(6)).build();softAssertions.assertThat(score.getValue()).as("Has score").isEqualTo(8);softAssertions.assertThat(score.getCombination()).as("Has combination").isEqualTo(dice(1, 1, 3, 3));softAssertions.assertThat(score.getReminder()).as("Has reminder").isEqualTo(dice(5)); }我們不僅不需要記住調用assertAll()而且還可以在IntelliJ的比較編輯器中看到潛在的失敗:
自定義
為了提高分數驗證的可讀性和可重用性,我們可以創建一個自定義斷言,以便可以按以下方式使用它:
@Test public void verifiesScoreSoftlyWithCustomAssertion() {Score score = Score.scoreBuilder().withValue(11).withCombination(dice(1, 1, 3, 4)).withReminder(dice(6)).build();SoftScoreAssertion.assertThat(score).hasValue(8).hasCombination(dice(1, 1, 3, 3)).hasReminder(dice(5)).assertAll(); }SoftScoreAssertion使用SoftAssertions ,因此我們仍然會立即看到所有斷言錯誤。 和代碼:
class SoftScoreAssertion extends AbstractAssert<SoftScoreAssertion, Score> {private SoftAssertions softAssertions = new SoftAssertions();protected SoftScoreAssertion(Score actual) {super(actual, SoftScoreAssertion.class);}public static SoftScoreAssertion assertThat(Score actual) {return new SoftScoreAssertion(actual);}public SoftScoreAssertion hasValue(int scoreValue) {isNotNull();softAssertions.assertThat(actual.getValue()).as("Has score").isEqualTo(scoreValue);return this;}public SoftScoreAssertion hasReminder(List<Dice> expected) {isNotNull();softAssertions.assertThat(actual.getReminder()).as("Has reminder").isEqualTo(expected);return this;}public SoftScoreAssertion hasCombination(List<Dice> expected) {isNotNull();softAssertions.assertThat(actual.getCombination()).as("Has combination").isEqualTo(expected);return this;}@Overridepublic SoftScoreAssertion isNotNull() {softAssertions.assertThat(actual).isNotNull();return this;}public void assertAll() {this.softAssertions.assertAll();} }資源資源
- http://joel-costigliola.github.io/assertj/assertj-core-features-highlight.html#soft-assertions
- https://github.com/joel-costigliola/assertj-core/wiki/Creating-specific-assertions
源代碼
- 可以在我在GitHub上的unit-testing-demo項目中找到本文的源代碼: https : //github.com/kolorobot/unit-testing-demo 。
翻譯自: https://www.javacodegeeks.com/2015/09/assertjs-softassertions-do-we-need-them.html
assertj
總結
以上是生活随笔為你收集整理的assertj_AssertJ的SoftAssertions –我们需要它们吗?的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 初始号是什么意思 初始号意思是什么
- 下一篇: 国歌叫什么 国歌具体叫什么