You can split them very easily using JUnit categories and Maven.
This is shown very, very briefly below by splitting unit and integration tests.
Define A Marker Interface
The first step in grouping a test using categories is to create a marker interface.
This interface will be used to mark all of the tests that you want to be run as integration tests.
public interface IntegrationTest {}
Mark your test classes
Add the category annotation to the top of 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 {
}
}
Configure Maven Unit Tests
The beauty of this solution is that nothing really changes for the unit test side of things.
We simply add some configuration to the maven surefire plugin to make it to ignore any integration tests.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.11</version>
<configuration>
<includes>
<include>**/*.class</include>
</includes>
<excludedGroups>
com.test.annotation.type.IntegrationTest
</excludedGroups>
</configuration>
</plugin>
When you do a mvn clean test
, only your unmarked unit tests will run.
Configure Maven Integration Tests
Again the configuration for this is very simple.
We use the standard failsafe plugin and configure it to only run the integration tests.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<version>2.19.1</version>
<configuration>
<includes>
<include>**/*.class</include>
</includes>
<groups>
com.test.annotation.type.IntegrationTest
</groups>
</configuration>
</plugin>
The configuration uses a standard execution goal to run the failsafe plugin during the integration-test phase of the build.
You can now do a mvn clean install
.
This time as well as the unit tests running, the integration tests are run during the integration-test phase.