【问题标题】:Exclude enum classes from code coverage?从代码覆盖范围中排除枚举类?
【发布时间】:2017-02-15 09:13:24
【问题描述】:

到目前为止,我根据它们的包名或类名来排除某些类。有什么方法可以将下面给出的枚举排除在覆盖范围之外?

package com.*;

public enum Quality {
    high,normal,low;
}

对于 Coverage,我将 maven-surefire-plugin 与 JUnit 和 JMockit 一起使用。我的覆盖设置如下所示

<build>
<plugins>
....
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>2.18.1</version>
    <dependencies>
        <dependency>
            <groupId>org.apache.maven.surefire</groupId>
            <artifactId>surefire-junit4</artifactId>
            <version>2.18.1</version>
        </dependency>
    </dependencies>
    <configuration>
        <systemPropertyVariables>
            <coverage-excludes>com.test.*,*IT.java</coverage-excludes>
        </systemPropertyVariables>
    </configuration>
</plugin>   
....
</build>
<dependencies>
    ....
        <dependency>
            <groupId>org.jmockit</groupId>
            <artifactId>jmockit</artifactId>
            <version>1.17</version>
        </dependency>
        <dependency>
            <groupId>org.jmockit</groupId>
            <artifactId>jmockit-coverage</artifactId>
            <version>1.17</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.11</version>
        </dependency>
    ....    
  </dependencies>

【问题讨论】:

  • 为什么要排除枚举?当然,如果您的某些生产代码使用这些枚举,它们将被覆盖,否则它会指示您未使用某个值。

标签: java maven junit jmockit maven-surefire-plugin


【解决方案1】:

排除属性接受正则表达式。您可以在应用程序中遵循代码约定,以便所有枚举的名称中都有一个共同的模式,例如 QualityEnum.java,然后您可以在排除中应用正则表达式过滤器

<exclude>%regex[*Enum.java], com.test.*</exclude>  

确保其他非枚举类避免这种约定。或者,在每个模块中有一个专门的子包用于枚举,并排除子包

【讨论】:

  • 是的,我可以做到。但我想根据它的内容而不是名称后修复来排除。如果我想排除,比如说抽象类,引入另一个后修复模式怎么办?但是很好的建议!
  • 我认为这将涉及为万无一失编写一个新的排除过滤器实现,它可以执行 InstanceOf 操作@mavi
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-02-12
  • 2016-03-21
  • 2016-03-29
  • 2015-04-27
  • 1970-01-01
  • 1970-01-01
  • 2022-10-21
相关资源
最近更新 更多