当前位置:
首页 >
assertj断言异常_编写自定义的AssertJ断言
发布时间:2023/12/3
49
豆豆
生活随笔
收集整理的这篇文章主要介绍了
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断言的全部内容,希望文章能够帮你解决所遇到的问题。
- 上一篇: 中国信创电脑系统(信创电脑操作系统)
- 下一篇: jdk 细粒度锁_使用JDK 8轻松进行