【问题标题】:Exclude Setters and Getters in JaCoCo Code Coverage在 JaCoCo 代码覆盖范围内排除 Setter 和 Getter
【发布时间】:2014-10-24 21:23:57
【问题描述】:

使用cobertura-maven-plugin,可以使用ignoreTrivial 选项将setter 和getter 排除在代码覆盖范围之外。 jacoco-maven-plugin也有这种可能吗?

这是我当前的配置:

<plugin>
  <groupId>org.jacoco</groupId>
  <artifactId>jacoco-maven-plugin</artifactId>
  <version>0.7.1.201405082137</version>
  <configuration>
    <excludes>
      <exclude>com/welovecoding/web/blog/test/**/*.class</exclude>
    </excludes>
  </configuration>
  <executions>
    <execution>
      <id>amend-unit-test-java-agent-option</id>
      <goals>
        <goal>prepare-agent</goal>
      </goals>
    </execution>
    <execution>
      <id>report</id>
      <phase>prepare-package</phase>
      <goals>
        <goal>report</goal>
      </goals>
    </execution>
  </executions>
</plugin>

【问题讨论】:

  • 你还在寻找答案吗,可能是Gradle
  • 是的,我还在寻找答案!! :-)
  • 当你有包时 some/package/model/**/*some/package/dto/**/* 怎么样包含大部分琐碎的代码?

标签: code-coverage setter getter jacoco jacoco-maven-plugin


【解决方案1】:

从 JaCoCo 0.8.0 开始,可以从覆盖率指标中排除 Lombok 自动生成的 getter / setter(以及 toString()equals()、...),这要归功于filtering options

龙目岛

使用 @lombok.Generated 注释的方法(由 Lombok getter、setter、equals、hashcode、toString 等生成)- 在 0.8.0 中完成

为此,您首先需要创建一个lombok.config 文件,例如位于项目的根文件夹中,其内容如下:

lombok.addLombokGeneratedAnnotation = true

@Generated注解会添加到setter/getter等,在覆盖分析中会被跳过。

【讨论】:

    【解决方案2】:

    官方不支持,见 cmets in :

    https://github.com/jacoco/jacoco/issues/15

    提到的解决方案:

    这个问题已经很久没有打开了。这是一个非常有趣的功能。例如,它在 clover 和 corbetura 中实现。有实现过滤的叉子: github.com/huangxiwei/jacoco, https://github.com/mchr3k/jacoco 从年初开始。你为什么不把那些 fork 合并到 master 分支中呢?即使在开始时没有实现所有过滤,所需的主要过滤器也会在您编写的 wiki 页面中列出(尝试使用资源、同步块、枚举静态方法)。覆盖率是一个非常有用的工具,它越多,它就越有用。当覆盖率达到高值时,它会很有帮助,有助于专注于正确的课程。

    【讨论】:

    • 感谢分享该链接!我写信给 GitHub 问题。也许会有足够的票数来获得这个功能。
    【解决方案3】:

    我建议使用 Cobertura,它没有这种限制,也没有太多误报警告。

    【讨论】:

    • 误报什么?
    • Cobertura 社区几乎不存在,并且(充其量)对 java 8 的基本支持。
    猜你喜欢
    • 2016-03-21
    • 2016-03-29
    • 2018-04-05
    • 2018-07-27
    • 1970-01-01
    • 2021-07-10
    • 2018-08-01
    • 2015-07-27
    • 1970-01-01
    相关资源
    最近更新 更多