介绍
如果您不知道什么是软断言,请阅读软断言 – 为什么应该将它们用于单元和集成测试?
本文是 Assert with Grace: Custom Assertions for Cleaner Code 的延续,它向您展示了如何使用 AssertJ 创建自定义断言。在这里,您将学习如何扩展其方法以在自定义断言之上使用软断言方法。
使用 AssertJ 自定义软断言
您可以使用 AssertJ 中的 Assertions 类或自定义类进行硬断言。为了获得软断言的所有好处,我们需要:
- 实现自定义断言
- 创建自定义软断言类并从 AssertJ 扩展 AbstractSoftAssertions
自定义断言
您在 Assert with Grace: Custom Assertions for Cleaner Code 一文中了解了如何创建自定义断言。看起来像这样:
public class SimulationAssert extends AbstractAssert<simulationassert simulation> { protected SimulationAssert(Simulation actual) { super(actual, SimulationAssert.class); } public static SimulationAssert assertThat(Simulation actual) { return new SimulationAssert(actual); } public SimulationAssert hasValidInstallments() { isNotNull(); if (actual.getInstallments() = 48) { failWithMessage("Installments must be must be equal or greater than 2 and equal or less than 48"); } return this; } public SimulationAssert hasValidAmount() { isNotNull(); var minimum = new BigDecimal("1.000"); var maximum = new BigDecimal("40.000"); if (actual.getAmount().compareTo(minimum) 0) { failWithMessage("Amount must be equal or greater than $ 1.000 or equal or less than than $ 40.000"); } return this; } } </simulationassert>
自定义断言的使用在测试中显示出更多的易读性,并将测试有效值的责任发送给它:
class SimulationsCustomAssertionTest { @Test void simulationErrorAssertion() { var simulation = Simulation.builder().name("John").cpf("9582728395").email("john@gmail.com") .amount(new BigDecimal("1.500")).installments(5).insurance(false).build(); SimulationAssert.assertThat(simulation).hasValidInstallments(); SimulationAssert.assertThat(simulation).hasValidAmount(); } }
有了自定义断言,是时候实现自定义软断言了。
创建自定义软断言
创建自定义软断言的过程很简单,前提是实现自定义断言。鉴于上一篇文章,我们将SimulationAssert 类作为自定义断言,并将创建SimulationSoftAssert 作为自定义软断言。步骤如下:
- 扩展 AbstractSoftAssertions 类
- 使用以下命令创建assertThat()方法:
- 该方法返回一个对象作为自定义断言类
- 断言主题的参数
- 该方法返回方法代理,其中参数是自定义断言类和断言的主题
- 使用以下命令创建assertSoftly() 方法:
- 作为自定义软断言类的 Consumer 的参数
- 使用SoftAssertionsProvider.assertSoftly()方法,参数是自定义软断言类,方法参数
步骤看起来很复杂,但实际上,你最终会得到这样的结果:
public class SimulationSoftAssert extends AbstractSoftAssertions { public SimulationAssert assertThat(Simulation actual) { return proxy(SimulationAssert.class, Simulation.class, actual); } public static void assertSoftly(Consumer<simulationsoftassert> softly) { SoftAssertionsProvider.assertSoftly(SimulationSoftAssert.class, softly); } } </simulationsoftassert>
使用自定义软断言
AssertJ SoftAssertion 类负责软断言。这是适用于模拟上下文的示例:
AssertJ SoftAssertion 类负责软断言。这是适用于模拟上下文的示例:
public class SimulationAssert extends AbstractAssert<simulationassert simulation> { protected SimulationAssert(Simulation actual) { super(actual, SimulationAssert.class); } public static SimulationAssert assertThat(Simulation actual) { return new SimulationAssert(actual); } public SimulationAssert hasValidInstallments() { isNotNull(); if (actual.getInstallments() = 48) { failWithMessage("Installments must be must be equal or greater than 2 and equal or less than 48"); } return this; } public SimulationAssert hasValidAmount() { isNotNull(); var minimum = new BigDecimal("1.000"); var maximum = new BigDecimal("40.000"); if (actual.getAmount().compareTo(minimum) 0) { failWithMessage("Amount must be equal or greater than $ 1.000 or equal or less than than $ 40.000"); } return this; } } </simulationassert>
使用它的“问题”是我们将无法使用我们创建的自定义断言。在上面的示例中,您可以使用 isEqualTo() 查看分期付款和金额中的断言,因为 SoftAssertions 类无法访问自定义断言。
我们通过创建自定义软断言类解决了这个问题。因此,我们将使用自定义类:SimulationSoftAssert,而不是使用 SoftAssertions 类。
class SimulationsCustomAssertionTest { @Test void simulationErrorAssertion() { var simulation = Simulation.builder().name("John").cpf("9582728395").email("john@gmail.com") .amount(new BigDecimal("1.500")).installments(5).insurance(false).build(); SimulationAssert.assertThat(simulation).hasValidInstallments(); SimulationAssert.assertThat(simulation).hasValidAmount(); } }
SimulationSoftAssert.assertSoftly() 是软断言的提供者,它将调用所有内部方法以便能够在断言期间管理错误和其他活动。在assertSoftly() 内部使用的assertThat() 将是自定义断言,它将可以通过软断言和断言主题之间的proxy() 访问自定义断言。
使用这种方法,我们可以通过实现自定义断言来在软断言中使用自定义断言。
结束
这就是大家!
您可以在credit-api项目中找到一个完全实现且有效的示例,您可以在其中看到以下内容:
- 模拟断言类
- SimulationsCustomAssertionTest 类中的测试用法
以上是优雅断言:使用 AssertJ 实现更简洁的代码的自定义软断言的详细内容。更多信息请关注PHP中文网其他相关文章!

本文讨论了使用Maven和Gradle进行Java项目管理,构建自动化和依赖性解决方案,以比较其方法和优化策略。

本文使用Maven和Gradle之类的工具讨论了具有适当的版本控制和依赖关系管理的自定义Java库(JAR文件)的创建和使用。

本文讨论了使用咖啡因和Guava缓存在Java中实施多层缓存以提高应用程序性能。它涵盖设置,集成和绩效优势,以及配置和驱逐政策管理最佳PRA

本文讨论了使用JPA进行对象相关映射,并具有高级功能,例如缓存和懒惰加载。它涵盖了设置,实体映射和优化性能的最佳实践,同时突出潜在的陷阱。[159个字符]

Java的类上载涉及使用带有引导,扩展程序和应用程序类负载器的分层系统加载,链接和初始化类。父代授权模型确保首先加载核心类别,从而影响自定义类LOA


热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

AI Hentai Generator
免费生成ai无尽的。

热门文章

热工具

ZendStudio 13.5.1 Mac
功能强大的PHP集成开发环境

PhpStorm Mac 版本
最新(2018.2.1 )专业的PHP集成开发工具

Dreamweaver CS6
视觉化网页开发工具

VSCode Windows 64位 下载
微软推出的免费、功能强大的一款IDE编辑器

Dreamweaver Mac版
视觉化网页开发工具