DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
View Events Video Library
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Integrating PostgreSQL Databases with ANF: Join this workshop to learn how to create a PostgreSQL server using Instaclustr’s managed service

Mobile Database Essentials: Assess data needs, storage requirements, and more when leveraging databases for cloud and edge applications.

Monitoring and Observability for LLMs: Datadog and Google Cloud discuss how to achieve optimal AI model performance.

Automated Testing: The latest on architecture, TDD, and the benefits of AI and low-code tools.

Related

  • Database Integration Tests With Spring Boot and Testcontainers
  • 7 Awesome Libraries for Java Unit and Integration Testing
  • IntelliJ Integration for Mockito
  • Microservices Testing: Key Strategies and Tools

Trending

  • Snowflake vs. Data Bricks: Compete To Create the Best Cloud Data Platform
  • A Complete Guide to Open-Source LLMs
  • Leveraging FastAPI for Building Secure and High-Performance Banking APIs
  • Best Practices for Writing Clean Java Code
  1. DZone
  2. Testing, Deployment, and Maintenance
  3. Deployment
  4. Easy Unit and Integration Code Coverage

Easy Unit and Integration Code Coverage

John Dobie user avatar by
John Dobie
·
May. 17, 12 · Interview
Like (0)
Save
Tweet
Share
23.92K Views

Join the DZone community and get the full member experience.

Join For Free

This example shows how to generate coverage for unit and integration tests using Maven and Sonar.
It uses very simple techniques and should only take 10-15 minutes to get running in any existing Maven build.
It can be used across unit, integration, ATDD or any other kind of test suite.
The coverage results are shown in Sonar.


Whats Coming?

My previous post showed how we can use JUnit categories to easily split unit and integration test suites.
http://johndobie.blogspot.com/2012/04/unit-and-integration-tests-with-maven.html

The next logical step is to be able to look at metrics for each test suite.
This example shows how to do that using Jacoco and Sonar.

Code

The code for the example is here.
svn co https://designbycontract.googlecode.com/svn/trunk/examples/maven/categories-sonar
mvn clean install sonar:sonar


Sonar.

This example relies on Sonar to show the code coverage metrics. Sonar is a fanatastic open source code quality tool that everyone should have a look at.
http://www.sonarsource.org/

For our example there are a couple of simple configuration changes that are needed.
The following link shows how to install Sonar and make the changes
http://johndobie.blogspot.com/p/setting-up-sonar.html


Splitting The Test Suites.

This example relies on JUnit categories to split our tests.
We define a marker interface and then apply it to the tests we want to split.
public interface IntegrationTest {}
The category annotation is added to your test class. It takes the name of your new interface.
import org.junit.experimental.categories.Category;
@Category(IntegrationTest.class)
public class ExampleIntegrationTest{
 @Test
 public void longRunningServiceTest() throws Exception {
    
 }
}
The whole process is very simple and is fully explained here
http://johndobie.blogspot.com/2012/04/unit-and-integration-tests-with-maven.html


Analyzing The Code Coverage

We use the jacoco plugin to do the code coverage. There is an overview of Jacoco here.
http://johndobie.blogspot.com/2012/01/unit-test-code-coverage.html
We first define the directories for the jacoco coverage files.
<coverage.reports.dir>
  ${basedir}/target/coverage-reports
</coverage.reports.dir>
<sonar.jacoco.reportPath>
  ${coverage.reports.dir}/jacoco-unit.exec
</sonar.jacoco.reportPath>
<sonar.jacoco.itReportPath>
  ${coverage.reports.dir}/jacoco-it.exec
</sonar.jacoco.itReportPath>
<sonar.jacoco.jar>
  ${basedir}/lib/jacocoagent.jar
</sonar.jacoco.jar>


Configure the Unit Tests

Then we start the unit tests by running the standard the surefire plugin with the Jacoco agent pointing to ${sonar.jacoco.reportPath}.  This is used to store the unit test code coverage results.
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>2.7.2</version>
<configuration>
	<argLine>-javaagent:${sonar.jacoco.jar}=destfile=${sonar.jacoco.reportPath},includes=com.*</argLine>
	<includes>
		<include>**/*.class</include>
	</includes>
	<excludedGroups>com.test.annotation.type.IntegrationTest</excludedGroups>
</configuration>
</plugin>
we ignore any marked integration tests with the following config
<excludedGroups>com.test.annotation.type.IntegrationTest</excludedGroups>


Configure the integration tests

For the Integration tests we use the failsafe plugin and point the Jacoco agent to ${sonar.jacoco.itReportPath}.  This is used to store the integration test code coverage results.
<plugin>
<plugin>
	<artifactId>maven-failsafe-plugin</artifactId>
	<version>2.12</version>
	<dependencies>
		<dependency>
			<groupId>org.apache.maven.surefire</groupId>
			<artifactId>surefire-junit47</artifactId>
			<version>2.12</version>
		</dependency>
	</dependencies>
	<configuration>
		<groups>com.test.annotation.type.IntegrationTest</groups>
	</configuration>
	<executions>
		<execution>
			<goals>
				<goal>integration-test</goal>
			</goals>
			<configuration>
				<argLine>-javaagent:${sonar.jacoco.jar}=destfile=${sonar.jacoco.itReportPath},includes=com.*</argLine>
				<includes>
					<include>**/*.class</include>
				</includes>
			</configuration>
		</execution>
	</executions>
</plugin>
We also tell the plugin to use the correct JUnit categories
<configuration>
	<groups>com.test.annotation.type.IntegrationTest</groups>
</configuration>

When these are run they will produce the following 2 coverage files.



Start Sonar.

Before running the build you need to start your Sonar server.
http://johndobie.blogspot.com/p/setting-up-sonar.html


Running The Example

We can run the whole lot using the following command
mvn clean install sonar:sonar
You will see the following results if you browse to your sonar instance.

 

 

What's Next? 

The tests in this example leave a lot to be desired in terms of realism.

Now the technique has been covered I will do a more realistic example and post this soon. 

 

Code coverage Integration unit test

Opinions expressed by DZone contributors are their own.

Related

  • Database Integration Tests With Spring Boot and Testcontainers
  • 7 Awesome Libraries for Java Unit and Integration Testing
  • IntelliJ Integration for Mockito
  • Microservices Testing: Key Strategies and Tools

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends: