Posts Tagged ‘Cobertura’

Useful Eclipse Plugins

A list of handy plugins for the Eclipse IDE

  • eCobertura – Allows Cobertura code coverage to be run from the IDE directly
  • eclipse-cs – Enforces coding standards using the CheckStyle tool
  • PMD – Scans Java code and reports potential problems
  • M2Eclipse – Maven integration from Sonatype
    • m2e-extensions – Works with version 0.10 of the M2Eclipse plugin. Can automatically setup Checkstyle and PMD configuration for Eclipse to match their configuration from the pom file if they are in it
    • m2e-code-quality – Same as m2e-extensions, except this one works with version 0.12 of the M2Eclipse plugin

Note: both m2e-extensions and m2e-code-quality only work if you have the pmd and checkstyle plugins configured in the build section of the pom. They do not currently work if you have pmd and checkstyle configured in the reporting section of the pom.

Cobertura code coverage with Maven and PowerMock

If you use Powermock for unit testing with Maven and Cobertura for code coverage, you are likely to be affected by the following issue noted on the PowerMock issue tracker. If you did not have the maven-surefire-plugin forkmode configured to ‘pertest’, you would find that any tests annotated with @RunWith(PowerMockRunner.class) had zero percentage code coverage when you viewed the resulting code coverage report produced by the cobertura-maven-plugin. Whilst running with ‘pertest’ fixed the issue, it was very slow to run the Maven build compared against a forkmode of ‘once’.

In September 2009, Cobertura released version 1.9.3 and one of the changes mentioned was the following:

  • Support the case where multiple classloaders each load the Cobertura classes.

Seeing this, I decided to test if that would fix the issue. At present, the latest version of the cobertura-maven-plugin is 2.3 which runs with Cobertura 1.9.2. There are some snapshots in the codehaus snapshot repository which run with Cobertura 1.9.3.  However, I found that the snapshots were constantly changing, and each run of the Maven build downloaded the latest version and sometimes the latest version had issues. There is probably a way to stop this from happening but if you don’t want to use snapshots, there is a change you can make to the 2.3 version which will also fix the issue. The cobertura-maven-plugin-2.3.pom file needs to be edited. It can be found in your repository under the following directory: org/codehaus/mojo/cobertura-maven-plugin/2.3/

Find the dependencies section of the pom file and change the versions of Cobertura from 1.9.2 to 1.9.3. There are two instances that need changing:

<dependency>
	<groupId>net.sourceforge.cobertura</groupId>
	<artifactId>cobertura</artifactId>
	<version>1.9.3</version>
</dependency>
<dependency>
	<groupId>net.sourceforge.cobertura</groupId>
	<artifactId>cobertura-runtime</artifactId>
	<version>1.9.3</version>
	<type>pom</type>
</dependency>

With that change, you should find that you can safely remove the ‘pertest’ forkmode configuration since by default the surefire plugin will run with forkmode set to ‘once’, and your cobertura code coverage results should be reporting correctly.

UPDATE (26th April 2010):

There is actually a better way of doing this which doesn’t involve having to edit the pom file for the actual plugin.

In your pom file you will have defined the cobertura-maven-plugin in the reporting section. The reporting section does not allow to add dependencies to a plugin. However the build section does, and the plugins defined in the reporting section will also pick up those dependencies. Therefore, you can also define the cobertura-maven-plugin in the build section as a plugin. This will allow you to specify some dependencies, as shown below.

<build>
	<plugins>
		<plugin>
			<groupId>org.codehaus.mojo</groupId>
			<artifactId>cobertura-maven-plugin</artifactId>
			<version>2.3</version>
			<dependencies>
				<dependency>
					<groupId>net.sourceforge.cobertura</groupId>
					<artifactId>cobertura</artifactId>
					<version>1.9.4.1</version>
				</dependency>
				<dependency>
					<groupId>net.sourceforge.cobertura</groupId>
					<artifactId>cobertura-runtime</artifactId>
					<version>1.9.4.1</version>
					<type>pom</type>
				</dependency>
			</dependencies>
		</plugin>
    </plugins>
</build>

This will then use cobertura 1.9.4.1.  Any version above 1.9.2 should fix the issue, but 1.9.4.1 is currently the latest and supposedly a lot faster than previous versions.

UPDATE (11th May 2010):

Good news! There is now version 2.4 of the cobertura-maven-plugin. This uses 1.9.4.1 version of Cobertura, so none of the above is now necessary.