简体   繁体   English

如何从 jacoco 代码覆盖范围中排除一行?

[英]How to exclude a line from jacoco code coverage?

How would i exclude inputStream.close() from jacoco code coverage, in pom.xml or in the java code?我如何从 jacoco 代码覆盖范围、pom.xml 或 java 代码中排除inputStream.close()

public void run() {
    InputStream inputStream = null;
    try {
        inputStream = fileSystem.newFileInputStream(file);
    }
    finally {
        if(inputStream != null) {
            try {
                inputStream.close();
            } catch (IOException e) {}
        }
    }
}

I suspect what you're really aiming for is 100% coverage. 我怀疑你真正的目标是100%覆盖率。 Consider re-writing the code using a try-with-resources block instead. 考虑使用try-with-resources块重写代码。 For example: 例如:

try (final InputStream inputStream = new FileInputStream(file)){
    //work with inputStream; auto-closes
}
catch (final Exception ex){
    //handle it appropriately
}

As for now, there's no ability to exclude a specific line (see the link ): 至于现在,没有能力排除特定的行(参见链接 ):

As of today JaCoCo core only works on class files, there is no source processing . 截至今天,JaCoCo核心仅适用于类文件, 没有源处理 This would require a major rework of the architecture and adds additional configuration hassles. 这将需要对架构进行重大修改并增加额外的配置麻烦。

It means, Jacoco analyzes byte code of your program, not your sources, as the result it cannot use hints like comments . 这意味着,Jacoco分析您的程序的字节代码,而不是您的来源,因为它不能使用评论等提示

Follow the corresponding issue to track the status of such feature implementation. 按照相应的问题跟踪此功能实现的状态。

As a workaround you can put it into a separate method, but see, it's a bad smell when you change your code just to reach 100% coverage level. 作为一种解决方法,您可以将其置于一个单独的方法中,但是,当您将代码更改为达到100%覆盖率时,这是一种难闻的气味。

I don't think there is a way to exclude a particular statement. 我认为没有办法排除特定的陈述。 However, there is a provision to exclude a method, though its not recommended .As a bad workaround we can create method out of statement. 但是,有一个规定可以排除方法, though its not recommended 。作为一种不好的解决方法,我们可以创建语句之外的方法。 The new feature has been added in the 0.8.2 release of JaCoCo which filters out the methods annotated with @Generated . 新功能已添加到JaCoCo的0.8.2版本中,该版本过滤掉了使用@Generated注释的方法。 For details please see the documentation below: 有关详细信息,请参阅以下文档:

Classes and methods annotated with runtime visible and invisible annotation whose simple name is Generated are filtered out during generation of report 在生成报告期间过滤掉使用简单名称为Generated的运行时可见和不可见注释注释的类和方法

Refer https://github.com/jacoco/jacoco/wiki/FilteringOptions#annotation-based-filtering for more information. 有关更多信息,请参阅https://github.com/jacoco/jacoco/wiki/FilteringOptions#annotation-based-filtering

Well you can't. 嗯,你不能。 If you want to exclude from coverage some line which drops down the class coverage below some mandatory limit, then just exclude whole class or package. 如果您想要从覆盖范围中排除一些下降类覆盖率低于某个强制限制的行,那么只需排除整个类或包。 You can find more information here: Maven Jacoco Configuration - Exclude classes/packages from report not working 您可以在此处找到更多信息: Maven Jacoco配置 - 从报告中排除类/包不起作用

Possibly make a line to ignore a separate method.可能做一行来忽略一个单独的方法。 You can exclude a method by annotating it.您可以通过注释来排除方法。

I used this post: How would I add an annotation to exclude a method from a jacoco code coverage report?我使用了这篇文章: How would I add an annotation to exclude a method from a jacoco code coverage report?

because I wanted to exclude the initBinder() Spring callback in a RestController.因为我想排除 RestController 中的initBinder() Spring 回调。

So I have this:所以我有这个:

  @ExcludeFromJacocoGeneratedReport
  @InitBinder
  private void initBinder(WebDataBinder binder) {
      binder.setValidator(myInjectedDtoValidator);
  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM