4

Till now I'm excluding certain classes depending on their package names or class names. Is there any way I can exclude Enums like given below from coverage?

package com.*;

public enum Quality {
    high,normal,low;
}

For Coverage I'm using maven-surefire-plugin with JUnit and JMockit. And my coverage setup looks like this

<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>
Kaleb
  • 616
  • 1
  • 9
  • 22
  • why do you want to exclude enum? surely if some of your production code uses these enum, they will be covered otherwise it gives you an indication that a certain value is not used. – GauravJ Feb 15 '17 at 12:05

1 Answers1

1

The exclusion property accepts a regular expression. You can follow a code convention in your application such that all enums have a common pattern in their name such as QualityEnum.java, then you can apply a regex filter in the exclude

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

Make sure other non-enum classes avoid this convention. Alternately, have a dedicated sub-package in each module for enums, and exclude the sub-package

Monish Sen
  • 1,773
  • 3
  • 20
  • 32
  • yes I can do this. But I want to exclude depending on it's content not name post-fix.What if I want to exclude,let's say, abstract classes,introduce another post fix pattern?but good suggestion! – Kaleb Feb 16 '17 at 12:30
  • 1
    I would assume this would involve writing a new exclude filter implementation for surefire, that could do InstanceOf operations @mavi – Monish Sen Feb 16 '17 at 12:35