Skip to main content

Notice: this Wiki will be going read only early in 2024 and edits will no longer be possible. Please see: https://gitlab.eclipse.org/eclipsefdn/helpdesk/-/wikis/Wiki-shutdown-plan for the plan.

Jump to: navigation, search

AspectJMaven/cobertura

You can use Cobertura to check test coverage of a project built with Maven and AspectJ.

In the following POM file, we used Cobertura in combination with the AspectJ Maven Plugin :

<project>
    [...]
        <build>
                <plugins>
                        <!-- Configure the ApsectJ Maven Plugin -->
                        <plugin>
                                <groupId>org.codehaus.mojo</groupId>
                                <artifactId>aspectj-maven-plugin</artifactId>
                                <executions>
                                        <execution>
                                                <goals>
                                                        <goal>compile</goal>
                                                        <goal>test-compile</goal>
                                                </goals>
                                        </execution>
                                </executions>
                                <configuration>
                                        <source>1.5</source>
                                        <target>1.5</target>
                                </configuration>
                        </plugin>
                </plugins>
        </build>
        <reporting>
                <plugins>
                        <!-- This is the standard reporting plugin -->
                        <plugin>
                                <artifactId>maven-project-info-reports-plugin</artifactId>
                        </plugin>
                        <!-- This is the Cobertura reporting plugin -->
                        <plugin>
                                <groupId>org.codehaus.mojo</groupId>
                                <artifactId>cobertura-maven-plugin</artifactId>
                        </plugin>
                </plugins>
        </reporting>
        <dependencies>
                <dependency>
                        <groupId>junit</groupId>
                        <artifactId>junit</artifactId>
                        <version>4.7</version>
                        <scope>test</scope>
                </dependency>
                <dependency>
                        <groupId>org.aspectj</groupId>
                        <artifactId>aspectjrt</artifactId>
                        <version>1.6.6</version>
                </dependency>
        </dependencies>

</project>

Now, launching the maven site goal :

mvn clean site

Will in turn :

  • Clean
  • Build files found in src/main/java using AspectJ (including .aj aspects)
  • Build files found in src/test/java using AspectJ (including .aj aspects used only during tests)
  • Instrument with Cobertura the main classes AND aspects
  • Run the tests using Cobertura instrumentation
  • Collects Cobertura instrumentation results
  • Build the HTML site including the "Cobertura Coverage Report".


Cobertura will correctly instrument also aspects, however a few minor problems arise :

  • since AspectJ inlines a few calls inside methods using "line 1" of the target class file, or the first line of the method, you'll see cobertura reporting a lot of hits on this lines, even if they does not contain any apparently useful code.
  • since AspectJ places a few calls to obtain aspect instances, and a few checks, you'll see Cobertura giving a "red marker" indicating that at a certain line there have been partial code execution, while in fact it is not possible looking at the java code, unless you know that AspectJ is placing an "if" there.


To reduce the number of false positive/false negatives you have in the Cobertura report, you can use ignores and excludes to avoid coverage on AspectJ internal classes, AspectJ generated closures, and common AspectJ boilerplate code. Also, when performing code coverage, line numbers are extremely important, so using the -XnoInline option during your weaving process will preserve line numbers :


                        <plugin>
                                <groupId>org.codehaus.mojo</groupId>
                                <artifactId>aspectj-maven-plugin</artifactId>
                                <executions>
                                        <execution>
                                                <goals>
                                                        <goal>compile</goal>
                                                        <goal>test-compile</goal>
                                                </goals>
                                        </execution>
                                </executions>
                                <configuration>
                                        <source>1.5</source>
                                        <target>1.5</target>
                                        <XnoInline>true</XnoInline>
                                </configuration>
                        </plugin>
[...]

                        <plugin>
                                <groupId>org.codehaus.mojo</groupId>
                                <artifactId>cobertura-maven-plugin</artifactId>
                                <configuration>
                                        <instrumentation>
                                                <ignores>
                                                        <ignore>org.aspectj.runtime.*</ignore>
                                                </ignores>
                                                <excludes>
                                                        <exclude>**/*$AjcClosure*</exclude>
                                                </excludes>
                                        </instrumentation>
                                </configuration>
                        </plugin>

For better runtime performance however, the -XnoInline option should not be used during normal build, so it would be better to add that configuration on a separate profile you use during the developement, test and site phases.

Back to the top