AssertJ的SoftAssertions –我们需要它们吗?
編寫(xiě)好的單元測(cè)試的規(guī)則之一是,它應(yīng)該出于某種原因而失敗,因此,單元測(cè)試應(yīng)該測(cè)試一種邏輯概念。 有時(shí)很難在每個(gè)測(cè)試中擁有一個(gè)斷言。 為了遵循規(guī)則,我們可能在一個(gè)測(cè)試中每個(gè)對(duì)象具有多個(gè)斷言。
但是,在單個(gè)測(cè)試中存在多個(gè)斷言的問(wèn)題在于,如果第一個(gè)斷言由于任何原因而失敗,我們實(shí)際上將不知道其他斷言,因?yàn)樗鼈儗⒉粫?huì)被執(zhí)行。 并且您知道了演練:您檢查斷言失敗原因,進(jìn)行修復(fù),然后重新運(yùn)行測(cè)試。 也許您很幸運(yùn),測(cè)試會(huì)通過(guò)。 但是也許它將因另一個(gè)斷言而失敗。 對(duì)于真正快速的單元測(cè)試,這不是什么大問(wèn)題,但是例如,在進(jìn)行硒測(cè)試時(shí),分析和故障檢測(cè)可能會(huì)變得很麻煩并且肯定會(huì)很費(fèi)時(shí)。
幸運(yùn)的是,借助AssertJ的SoftAssertions ,我們可以重新考慮在測(cè)試中創(chuàng)建斷言的SoftAssertions 。
一個(gè)宣稱可以統(tǒng)治所有人的主張!
在假設(shè)的Dice游戲中,有一個(gè)Score對(duì)象,其中保存得分值,骰子組合和提醒。 在單元測(cè)試中,我們可能想驗(yàn)證不同骰子組合的分?jǐn)?shù)計(jì)算方式。
在下面的示例中,驗(yàn)證了一個(gè)概念(得分對(duì)象):
@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)); }如您所見(jiàn),所有三個(gè)斷言都失敗了,但是由于第一個(gè)失敗后測(cè)試的執(zhí)行停止,因此我們只會(huì)看到第一個(gè)失敗的結(jié)果:
org.junit.ComparisonFailure: [Has score] Expected :8 Actual :11引入
為了解決這個(gè)問(wèn)題,我們可以使用SoftAssertions ,它將在調(diào)用assertAll()方法時(shí)立即收集所有斷言的結(jié)果:
@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(); }現(xiàn)在我們可以驗(yàn)證測(cè)試中的所有斷言失敗:
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
代替手動(dòng)創(chuàng)建SoftAssertions并調(diào)用其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)); }我們不僅不需要記住調(diào)用assertAll()而且還可以在IntelliJ的比較編輯器中看到潛在的失敗:
自定義
為了提高分?jǐn)?shù)驗(yàn)證的可讀性和可重用性,我們可以創(chuàng)建一個(gè)自定義斷言,以便可以按以下方式使用它:
@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 ,因此我們?nèi)匀粫?huì)立即看到所有斷言錯(cuò)誤。 和代碼:
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項(xiàng)目中找到本文的源代碼: https : //github.com/kolorobot/unit-testing-demo 。
翻譯自: https://www.javacodegeeks.com/2015/09/assertjs-softassertions-do-we-need-them.html
總結(jié)
以上是生活随笔為你收集整理的AssertJ的SoftAssertions –我们需要它们吗?的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 阿里云备案服务号在哪里买(阿里云备案号购
- 下一篇: 记录链接:与杜克一起玩