SpringBoot Separate Unit & Integration Test

Vivek Singh
3 min readMar 14, 2022

--

Git code available

In this article, we will see how we can separate our unit and integration tests in springboot. Unit Test and Integration tests can be run separately using separate commands. This can be used for different purposes like in a CICD pipeline where we have two separate stage called ‘unit test’ and ‘integration test’.

What is a Unit Test ?

A type of testing method where independent classes or modules are tested to check for any error in code. These tests are boundary centric like testing only one layer of an application like Controller, Service or Dao layer.

What is a Integration Test ?

The purpose of Integration testing is to find errors in interaction between two logical/functional units. When you write integration testing code, it means the developers have already tested the smaller units while unit testing. So now the whole idea is to test two functional components together like testing the service layer and dao layer together or testing the end to end working of a REST API endpoint. Integration testing is usually performed by Testers, however being a developer I have worked on Integration testing applications written on Java completely. So the functionality and business use case decides who owns the integration testing part.

What is a Profile in Maven ?
Maven profiles can be used to create customized build configurations, like targeting a specific deployment environment, overriding the default mvn commands by creating a profile where running ‘mvn clean verify’ can skip test case runs. In our case, we will use Maven profiles to separate unit test and integration tests.

Check this stackoverflow link to see all the other possible ways to do it :

The above link mainly focuses on 2 plugins to get the job done : maven-surefire-plugin and maven-failsafe-plugin.

I ran into issue while following the above approach so I thought of keeping things simple .

My approach :

  1. Create two profiles using <profile> tag in maven named unit-test and integration-test.
  2. Use a naming convention for all your unit-tests and integration-tests. Example all your unit test starts with Test keyword (Test*.java) and integration tests with keyword IntegrationTest (IntegrationTest*.java)
    This way when you use profile commands it only runs files starting with specific name provided by <include> tag in profiles.
    Screenshot below :

3. Use the profiles name to run your unit test and integration test separately.

mvn test -Punit-test // profile name is unit-test for unit tests
mvn test -Pintegration-test //// profile name is integration-test for integration tests

This is how you create those two profiles :

<profiles>
<profile>
<id>unit-test</id>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<includes>
<include>**/Test*.java</include>
</includes>
</configuration>
</plugin>
</plugins>
</build>
</profile>
<profile>
<id>integration-test</id>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<includes>
<include>**/IntegrationTest*.java</include>
</includes>
</configuration>
</plugin>
</plugins>
</build>
</profile>

</profiles>

In the above code, check the include tag, because this is what helps profiles to run unit and integration test cases separately.

GIT CODE

Happy learning. Cheers. Please follow and like this to get more content from me. Feel free to write me on any freelancing work or just a casual conversation . :)

--

--

Vivek Singh
Vivek Singh

Written by Vivek Singh

Software Developer. I write about Full Stack, NLP and Blockchain. Buy me a coffee - buymeacoffee.com/viveksinless

No responses yet