Skip to main content

Notice: This Wiki is now read only and edits are no longer possible. Please see: https://gitlab.eclipse.org/eclipsefdn/helpdesk/-/wikis/Wiki-shutdown-plan for the plan.

Jump to: navigation, search

AspectJMaven/cobertura

< AspectJMaven
Revision as of 12:07, 29 December 2009 by Unnamed Poltroon (Talk) (New page: 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 : <pre> ...)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

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.

Back to the top