Junit在我的测试案例中断言OR条件 [英] Junit assert OR condition in my test case

查看:93
本文介绍了Junit在我的测试案例中断言OR条件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

In my test case, I get an integer value:

int val = getXXX();

然后,我想检查val是等于3还是等于5,在两种情况下都可以.所以,我做到了:

Then, I would like to check if val either equals to 3 or equals to 5 which is OK in either case. So, I did:

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

我运行测试,日志显示val为5,但是我的上述声明代码失败,出现 AssertionFailedError .似乎我不能以这种方式使用assertTrue(...),那么,如何检查OR条件为true?

I run my test, the log shows val is 5, but my above assertion code failed with AssertionFailedError. Seems I can not use assertTrue(...) in this way, then, how to check true for OR condition?

推荐答案

ive试图编写快速测试:

ive tried to write quick test:

@Test
public void testName() {
    int i = 5;
    junit.framework.Assert.assertTrue(i == 3 || i == 5);

}

它总是传递过去的,所以我猜当你的值改变时,在代码之间会有一些区别. 您可以使用

its passing always so i guess there is some inbetween code when your value is changed. You can use

org.junit.Assert.assertEquals(5, i);

检查值-此断言将打印出正确的信息,说明出了什么问题,例如:

to check value - this assertion will print out nice info whats wrong, for example:

java.lang.AssertionError: 
Expected :4
Actual   :5

这篇关于Junit在我的测试案例中断言OR条件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆