Java中的代码覆盖与EclEmma不扫描预期的异常方法 [英] Code coverage in Java with EclEmma not scanning expecting exception methods

查看:307
本文介绍了Java中的代码覆盖与EclEmma不扫描预期的异常方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使用Eclipse和EclEmma来获取我的代码覆盖率。



我的测试使用JUnit 4,我有一些测试看起来像这个:

  @Test(expected = IllegalArgumentException.class)
public void createTime_withInvalidMinuteUnder0_throws(){
/ /排列
...
// Act
触发IllegalArgumentException的东西
}

EclEmma表示测试失败,因为有一个IllegalArgumentException被抛出。所以它会丢弃我的代码覆盖率指标,即使它应该抛出一些东西。有没有可以让它看到JUnit预期的异常标记?



编辑:我已经发现,如果将throw添加到测试声明中,它的作品!

解决方案

不,没有办法让EclEmma注意到预期子句。他们承认此事实此处


为什么JUnit4测试用例中的预期异常显示为未覆盖?



JUnit4测试用例与预期异常显示为未覆盖即使他们被处决了。这样做的原因是基础的JaCoCo代码覆盖库仅考虑执行某些探针时执行的代码。对于标有 @Test {expected = ...} 的成功测试用例,情况并非如此。


我个人不用担心太多了。测试用例的覆盖是EclEmma可以告诉你的最不重要的事情;我总是完全忽视这些指标,并专注于我的生产代码的覆盖范围。


I'm trying to get my code coverage in java, using Eclipse and EclEmma.

My tests are using JUnit 4 and I've got some tests looking like this :

    @Test(expected = IllegalArgumentException.class)
    public void createTime_withInvalidMinuteUnder0_throws(){
    //Arrange
    ...
    //Act
    Something triggering IllegalArgumentException Here       
}

And EclEmma says that the test fails because there's an IllegalArgumentException being thrown. So it drops my code coverage indicator even though it's supposed to throw something. Is there an option to make it see that JUnit expected exception tag ?

edit: I've found out that if you add the throw to the declaration of the test aswell, it works!

解决方案

No, there is no way to get EclEmma to notice the expected clause. They acknowledge this fact here.

Why are JUnit4 test cases with expected exceptions shown as not covered?

JUnit4 test cases with expected exceptions are shown as not covered even though they were executed. The reason for this is that underlying JaCoCo code coverage library only considers code as executed when certain probes are executed. For successful test cases marked with @Test{expected=...} this is not the case.

Personally, I wouldn't worry too much about it. Coverage of test cases is the least interesting thing EclEmma can tell you; I always completely ignore those metrics and focus on the coverage of my production code.

这篇关于Java中的代码覆盖与EclEmma不扫描预期的异常方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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