Main Tutorials

Maven + Emma code coverage example

emma-logo

Emma is a free Java code coverage tool. In this tutorial, we will show you how to use Maven to generate the Emma code coverage report for your project, and also how to integrate the Emma report into the Maven project site.

1. Generate Emma Code Coverage Report

Do nothing, just type the following Maven command mvn emma:emma to run the maven-emma-plugin.


c:\project> mvn emma:emma

//...
Tests run: 16, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 1.53 sec
EMMA: locking coverage output file [C:\mkyong_projects\TestNG\coverage.ec] ...
EMMA: runtime coverage data merged into [C:\mkyong_projects\TestNG\coverage.ec] {in 78 ms}

Results :

Tests run: 16, Failures: 0, Errors: 0, Skipped: 0

[INFO]
[INFO] <<< emma-maven-plugin:1.0-alpha-3:emma (default-cli) @ TestNG <<<
[INFO]
[INFO] --- emma-maven-plugin:1.0-alpha-3:emma (default-cli) @ TestNG ---
processing input files ...
2 file(s) read and merged in 1 ms
writing [xml] report to [C:\mkyong_projects\TestNG\target\site\emma\coverage.xml] ...
writing [html] report to [C:\mkyong_projects\TestNG\target\site\emma\index.html] ...
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 4.004s
[INFO] Finished at: Fri Jan 10 23:32:05 SGT 2014
[INFO] Final Memory: 25M/307M
[INFO] ------------------------------------------------------------------------
C:\mkyong_projects\TestNG>

Maven will compile, run unit test and Emma plugin to generate the code coverage report at ${project}/target/site/index.html.

Figure : Sample of Emma code coverage report, ${project}/target/site/index.html.

emma-code-coverage-report-1

Figure : Detail page.

emma-code-coverage-report-2

2. Maven Site + Emma Report

To integrate Emma reports into the Maven project site, add the following to the reporting section.

pom.xml

  //...
  <reporting>
    <plugins>
	<!-- Normally, dependency report takes time, skip it -->
      <plugin>
	<groupId>org.apache.maven.plugins</groupId>
	<artifactId>maven-project-info-reports-plugin</artifactId>
	<version>2.7</version>

	<configuration>
          <dependencyLocationsEnabled>false</dependencyLocationsEnabled>
	</configuration>
      </plugin>

      // integrate maven emma plugin to project site 
      <plugin>
	<groupId>org.codehaus.mojo</groupId>
	<artifactId>emma-maven-plugin</artifactId>
	<version>1.0-alpha-3</version>
	<inherited>true</inherited>
      </plugin>

    </plugins>
  </reporting>

Creating Maven project site


c:\project> mvn site

//...
Tests run: 16, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 1.45 sec
EMMA: locking coverage output file [C:\mkyong_projects\TestNG\coverage.ec] ...
EMMA: runtime coverage data merged into [C:\mkyong_projects\TestNG\coverage.ec] {in 38 ms}

Results :

Tests run: 16, Failures: 0, Errors: 0, Skipped: 0

//... Generating other reports
[INFO] Generating "Project Summary" report    
[INFO] Generating "Dependencies" report    
[INFO] Generating "EMMA Test Coverage" report   
processing input files ...
2 file(s) read and merged in 2 ms
writing [xml] report to [C:\mkyong_projects\TestNG\target\site\emma\coverage.xml] ...
writing [html] report to [C:\mkyong_projects\TestNG\target\site\emma\index.html] ...
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 8.488s
[INFO] Finished at: Fri Jan 10 23:43:58 SGT 2014
[INFO] Final Memory: 40M/1024M
[INFO] ------------------------------------------------------------------------

Output – Project site, ${project}/site/index.html

maven-site-emma

3. Integrate into the Maven Build

You also can include the “emma-maven-plugin” in the build section.

pom.xml

  //...
  <build>
		
    <plugins>

	<plugin>
		<groupId>org.codehaus.mojo</groupId>
		<artifactId>emma-maven-plugin</artifactId>
		<version>1.0-alpha-3</version>
		<inherited>true</inherited>
		<executions>
		  <execution>
			<phase>process-classes</phase>
			<goals>
				<goal>instrument</goal>
			</goals>
		  </execution>
		</executions>
	 </plugin>
			
    </plugins>
  </build>

Just beware of the class X appears to be instrumented already error.

References

  1. Maven – Creating a site
  2. Emma code coverage tool
  3. Emma Maven Plugin
  4. Java Code Coverage Tools
  5. Maven + Cobertuna Code Coverage Example

About Author

author image
Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter. If you like my tutorials, consider make a donation to these charities.

Comments

Subscribe
Notify of
3 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments
karna
9 years ago

can you please give me once example source code

Shobana VK
4 years ago

Hi,
I tried your methods to generate Emma coverage Report. But i when i click on index.html and navigate to Emma Coverage Report, the page can’t be displayed is showing up. I have posted question on this in stack overflow.

https://stackoverflow.com/questions/56222110/emma-coverage-report-not-generated

galica
10 years ago

And how to run the tests for several Maven modules