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 –我们需要它们吗?相关推荐

  1. AssertJ的SoftAssertions –我们需要它们吗?

    编写好的单元测试的规则之一是,它应该出于某种原因而失败,因此,单元测试应该测试一种逻辑概念. 有时很难在每个测试中拥有一个断言. 为了遵循规则,我们可能在一个测试中每个对象具有多个断言. 但是,在单个 ...

  2. 推荐2一个在Java编码过程中得心应手的工具

    推荐2在编码过程中的减小不仅编码的量,挺easy工具上手:可适用Java反思与单探头Assert. 1 Mirror:Java反思 简单介绍 官网:http://projetos.vidageek.n ...

  3. maven配置junit5_JUnit 5和Selenium –改善项目配置

    maven配置junit5 Selenium是一组支持浏览器自动化的工具和库,主要用于Web应用程序测试. Selenium的组件之一是Selenium WebDriver,它提供客户端库,JSON有 ...

  4. junit5和junit4_JUnit 5符合AssertJ

    junit5和junit4 JUnit 5在断言库中带来了很多改进,这主要归功于Java 8和Lambda Expression的支持以及新断言(如assertAll , assertTimeout或 ...

  5. fest556_AssertJ Fest Hamcrest

    fest556 我以前在博客中介绍过Hamcrest ,并优先使用其assertThat方法而不是JUnit的Assert . 但是,我很快找到了FEST断言 ,并愉快地切换到它. 它提供了与Hamc ...

  6. JUnit 5和Selenium –改善项目配置

    Selenium是一组支持浏览器自动化的工具和库,主要用于Web应用程序测试. Selenium的组件之一是Selenium WebDriver,它提供客户端库,JSON有线协议(与浏览器驱动程序进行 ...

  7. JUnit 5符合AssertJ

    JUnit 5在断言库中带来了很多改进,这主要归功于Java 8和Lambda Expression支持以及新断言(如assertAll , assertTimeout或assertThrows . ...

  8. AssertJ Fest Hamcrest

    我以前曾在博客中介绍过Hamcrest ,并使用其assertThat方法优先于JUnit的Assert . 但是,我很快发现了FEST断言 ,并愉快地切换到它. 它提供了与Hamcrest相同的改进 ...

  9. fest3d_AssertJ Fest Hamcrest

    fest3d 我以前曾写过有关Hamcrest的博客,并优先使用其assertThat方法而不是JUnit的Assert . 但是,我很快找到了FEST断言,并愉快地切换到它. 它提供了与Hamcre ...

最新文章

  1. LeetCode简单题之数组形式的整数加法
  2. 前、后端分离权限控制设计和实现思路
  3. 他们说头不铁,别做机器人
  4. Windows7 WIN 7 64位 环境编译6sv2.1版本的大气传输模型
  5. String 常量池
  6. SpringBoot2.x整合Redis实战 4节课
  7. C++ 泛型编程(一):模板基础:函数模板,类模板,模板原理,模板匹配规则
  8. mysql 表丢失_Mysql数据库备份 部分数据表丢失 Mysql table doesn't exist 解决
  9. 蚁群算法python_想要学习启发式算法?推荐你看看这个价值极高的开源项目
  10. 仓储rfid文件_RFID技术在智能制造模具管理中的应用
  11. C语言函数多个返回值
  12. mysql 卡住_一次sql卡住的解决过程(mysql)
  13. 萌新接触前端的第三课——JavaScript
  14. php sockets有什么用,PHP中Sockets与流有什么关系啊!!!!!
  15. Ubuntu18.04使用记录
  16. 下载instagram
  17. Print Conductor中文版
  18. 计算机网络自顶向下 1
  19. suse linux快捷键,Suse Linux整理大全:快捷键
  20. Android 仿微信群聊头像

热门文章

  1. P3159-[CQOI2012]交换棋子【费用流】
  2. P1160-队列安排【链表】
  3. ssl2290-潜水员【dp之二维费用】
  4. 【线段树】GSS5 - Can you answer these queries V(luogu-SPOJ 2916)
  5. 【树链剖分】Disruption P(luogu 4374)
  6. 动态规划训练22 [Milking Time POJ - 3616 ]
  7. Spark入门(六)Spark SQL shell启动方式(元数据存储在mysql)
  8. 分享10道常考Java面试题及答案
  9. JVM内存管理------垃圾搜集器精解
  10. Properties文件的XML格式