Junit中未涵盖的2个分支的偶像1 [英] eclemma 1 of 2 branch not covered in Junit
问题描述
JUnit文件
public class SomeClassTest {
...
@Test
public void testSomeContextFailure() throws Exception {
SomeClass sc = new SomeClass();
try {
sc.SomeContext(null,null);
fail("IllegalArg expected");
} catch (IllegalArgumentException e) {}
}
@Test
public void testSomeContextSuccess() throws Exception {
SomeClass sc = new SomeClass();
SomeContext in = new SomeContext();
in.setName("something");
in.setId("5");
in.setPoints(SomePoint.X);
try {
assertNotNull(sc.SomeContext(in,null));
} catch (Exception e) {}
}
}
Java文件
public class SomeClassTest {
@Autowired(required = true)
private InsuredDAO insuredDAO;
@Override
public context SomeContext(context c, unused u) throws Exception {
if(c == null)
throw new IllegalArgumentException();
insuredDAO.increaseValue(c);
if(c.getPoints() != null) {
...do something
}
return c;
}
在Java文件if(c == null)
中以黄色突出显示,并显示一条消息,指出未覆盖2个分支中的1个.
In java file if(c == null)
was highlighted yellow with message saying 1 of 2 branches not covered.
throw new IllegalArgumentException();
突出显示的绿色
insuredDAO.increaseValue(c);
此行及其下方的所有内容均为红色
Everything on and below this line is red
我想念什么? (两个都通过了JUnit测试,但是为什么不包括在内)?
What am I missing? (JUnit test was passed on both but why it isn't covered)?
推荐答案
可能为时已晚,但是如果有人遇到此问题...
第一:
您有两项测试,其中一项失败.作为唯一通过的测试,测试了空"值,后缀将if语句标记为部分覆盖". ->已对null检查进行了测试,但是没有使用给定的Object.
它不能像调试器那样工作,测试要运行到这一行,然后才失败,但是直到此阶段为止所做的一切都将被分析".难题只是分析完全运行(并成功)的测试.
第二:
在您的课堂上有insuredDAO.increaseValue(c);
,它是自动连接的.在我的测试中,我必须模拟此Object,否则它将在该行失败并显示NullPointerException
,因为在junit测试中未完成自动装配,因此insuredDAO
为null且方法调用将抛出NullPointerException
. >
应该会在您的IDE中弹出;)
Might be a bit too late, but if someone else comes across this...
First:
You have two tests of which one of them fails. As the only test, that went through, tested the "null" value, eclemma marks the if statement as "partially covered". -> null check was tested, but with the given Object, it was not.
It doesn't work that way like a debugger "the test ran up until this line, then it failed, but everything it did until this stage will be analyzed". eclemma just analyzes fully run (and succeeded) tests.
Second:
In your class you have insuredDAO.increaseValue(c);
, which is autowired. In my tests I have to mock this Object, otherwise it will fail with a NullPointerException
at that line because autowiring is not done in junit tests, therefore insuredDAO
is null and the method call will throw a NullPointerException
.
Should have popped up in your IDE, though ;)
这篇关于Junit中未涵盖的2个分支的偶像1的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!