Home » Core Java » junit » JUnit Integration Test Example

About Alvin Reyes

Alvin has an Information Technology Degree from Mapua Institute of Technology. During his studies, he was already heavily involved in a number of small to large projects where he primarily contributes by doing programming, analysis design. After graduating, he continued to do side projects on Mobile, Desktop and Web Applications.

JUnit Integration Test Example

1. Introduction

Integration tests are test cases that test highly coupled external services. A great example of this is services in a SOA environment. In this scheme we will create services (or micro services) that are usually deployed on a different container and only exposed specific implementation of it’s own, consumed by a more sophisticated system.

By rule of thumb, we always need to separate this kind of tests from the internal service of the application. This separation, in JUnit Test case perspective, is called “categorising”, a pretty straight forward definition, we categorise a specific test case by creating a mark on them using Java Interface class. In this example, I’ll be giving a simple example of how we can write and categorise test cases by using the @Category annotation.

2. Source

The following codes are the source codes made for this example. Link to this project can be found at the end of this article.

2.1 Java Test Interface

We first need to mark our test cases. This is for us to categorise the test case that it’s an Integration tests.


package com.areyes1.jgc.junit.integ;
public interface IntegrationTest {}

2.2 Test Case

After that, we will use the interface class to mark our test case as an Integration test.


package com.areyes1.jgc.junit.integ;

import org.junit.Before;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.areyes1.jgc.junit.integ.JUnitAssertEqualsServiceExample;
import com.areyes1.jgc.junit.integ.ServiceObject;

import static org.junit.Assert.assertEquals;
public class IntegrationTestSampleTest {

	private JUnitAssertEqualsServiceExample junitAssertEqualsServiceSample;
	private ServiceObject serviceObject;
	public void setData() {
		serviceObject = new ServiceObject();
		junitAssertEqualsServiceSample = new JUnitAssertEqualsServiceExample();

	public void testAssertEqualsFalse() {
		//	processed the item
		ServiceObject newServiceObject = new ServiceObject();
	public void testAssertEquals() {

	public void testAssertEqualsWithMessage() {
				"Same Object",
	public void testAssertEqualsFalseWithMessage() {
		ServiceObject newServiceObject = new ServiceObject();
				"Not the Same Object",

2.3 Maven Config

To ensure that this class will only be executed upon running the integration-test, we need to add the following configuration on our pom.xml


We added the configuration that all test cases, which are categorised under Integrate test, will be part of the integration-test goal.

2.4 Maven command

Run the following: mvn integration-test

3. Output

Here’s the output of our log file:

 T E S T S
Running com.areyes1.jgc.junit.integ.IntegrationTestSampleTest
Tests run: 4, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.073 sec


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

[INFO] ------------------------------------------------------------------------
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1.843 s
[INFO] Finished at: 2015-08-27T18:54:16+08:00
[INFO] Final Memory: 9M/107M
[INFO] ------------------------------------------------------------------------

4. Download the Eclipse project

This was an example of JUnit Integration Tests.

You can download the full source code of this example here: junit-integration-tests

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you our best selling eBooks for FREE!


1. JPA Mini Book

2. JVM Troubleshooting Guide

3. JUnit Tutorial for Unit Testing

4. Java Annotations Tutorial

5. Java Interview Questions

6. Spring Interview Questions

7. Android UI Design


and many more ....


Receive Java & Developer job alerts in your Area


Notify of

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Inline Feedbacks
View all comments