本文介绍了如何在AssertJ中否定断言?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用Hamcrest,可以轻松取消匹配器.例如.您可以编写这样的断言:

Using Hamcrest it is easily possible to negate a matcher. E.g. you can write an assertion like this:

assertThat("The dog bites Tom", not(stringContainsInOrder(Arrays.asList("Tom", "dog"))));

即使用org.hamcrest.core.IsNotorg.hamcrest.core.AnyOf匹配器,可以轻松合并或取消断言.

I.e. using the org.hamcrest.core.IsNot , org.hamcrest.core.AnyOf matchers it is easy to combine or negate assertions.

AssertJ中是否有等效项?我知道可以合并/取反Condition.但是普通的断言方法呢?例如.如果要测试字符串 not 是否仅由数字组成,即要否定以下断言,该怎么办:

Is there any equivalent in AssertJ?I know that it is possible to combine/negate Conditions. But what about normal assertion methods? E.g. what do you do if you want to test that a String does not consists only of digits, i.e. negate the following assertion:

assertThat("1234xxx5678").containsOnlyDigits();

推荐答案

不可能组合普通的断言方法,这是Hamcrest比AssertJ更灵活的地方.

It is not possible to combine normal assertions methods, this is an area where Hamcrest is more flexible than AssertJ.

在您的情况下,我将按照您的建议编写条件,或者将lambda与 matches 断言.

In your case I would write a Condition as you suggested or use a lambda with matches assertion.

这篇关于如何在AssertJ中否定断言?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-12 06:37