从代码覆盖中排除枚举类?

时间:2017-02-15 09:13:24

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

直到现在我根据他们的包名或类名排除某些类。有什么方法可以排除下面给出的Enums覆盖范围吗?

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>

1 个答案:

答案 0 :(得分:1)

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

中应用正则表达式过滤器
<exclude>%regex[*Enum.java], com.test.*</exclude>  

确保其他非枚举类避免此约定。或者,在每个模块中为枚举设置一个专用子包,并排除子包