我的测试用例中的 Junit 断言 OR 条件

2024-03-05

在我的测试用例中,我得到一个整数值:

int val = getXXX();

然后,我想检查一下是否val等于 3 或等于 5,这两种情况都可以。所以我做了:

assertTrue(val == 3 || val==5);

我运行测试,日志显示val是 5,但是我上面的断言代码失败了断言失败错误。看来我不能使用assertTrue(...)那么,如何检查 OR 条件为真呢?


您可以使用汉克雷斯特匹配者 http://code.google.com/p/hamcrest/wiki/Tutorial在此处获取更清晰的错误消息:

int i = 2;
assertThat(i, Matchers.either(Matchers.is(3)).or(Matchers.is(5))

or

int i = 2;
assertThat(i, Matchers.anyOf(Matchers.is(3),Matchers.is(5)));

这将更清楚地解释:

Expected: (is <3> or is <5>)
     but: was <2>

准确显示所提供的期望值和不正确的值。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

我的测试用例中的 Junit 断言 OR 条件 的相关文章

随机推荐