Search code examples
javaspringcode-coverageclover

Globally exclude all Spring JavaConfig from Clover code coverage


I use Spring JavaConfig to define my Spring configuration. Since unit tests use a different Spring configuration than production code, the production configuration shows up as 100% uncovered when I use Clover code coverage with unit tests.

I can use the @Configuration annotation to identify all these classes. Alternatively, I can use the @Bean annotation to identify all the methods within these classes.

Is there a Clover exclude or a code context filter that I can set up to globally exclude code by using these annotations? I use the maven-clover2-plugin to run Clover.


Solution

  • At the moment Clover allows to exclude code from coverage measurement using five methods:

    1. excluding entire file

      • in Ant it is <clover-setup> / <fileset> / <excludes> tag
      • in Maven it is <excludes>, <excludesFile> or <excludesList> option
    2. excluding certain methods

      • in Ant it is <clover-setup> / <methodContext> tag combined with the <clover-report> / <current> / <
      • in Maven it is not available directly - you'd have to use the <reportDescriptor> option to configure report format in an external XML file; the problem is with clover-setup which you'd also have to customise - maybe calling the Clover via maven-antrun-plugin could help here
    3. excluding certain statements

      • in Ant it is <clover-setup> / <statementContext> tag combined with the <clover-report> / <current> / <
      • in Maven you'd have to use the <reportDescriptor>
    4. excluding certain code blocks

      • in Ant it is <clover-report> / <current> / < with a predefined names of blocks, e.g. 'constructor' or 'switch' - see Using+Coverage+Contexts for more details
      • in Maven you'd have to use the <reportDescriptor>
    5. excluding arbitrary source lines

    You can put //CLOVER:OFF and //CLOVER:ON inline comments in the source code

    Unfortunately, at the moment it's not possible to exclude the given class. However, I can see few workarounds available:

    1. Add the //CLOVER:OFF and //CLOVER:ON comments around classes you want to exclude.

    2. Exclude entire files

    3. Treat these @Configuration classes as test code.

    This is a hack. Clover allows to declare which classes are test classes. The match is based on file name, class signature and test signature. See the <clover-setup> / <testsources> / <testclass> tag. In this testclass tag you can define a regular expression matching entire class signature. In your case it could be:

    <testclass annotation=".*@Configuration.*"/>
    

    While this will not disable code instrumentation for these classes, they would be treated as test code and they should not contribute to application code metrics.

    Note that Clover calculates code metrics for application and test code separately.

    References: