Run JUnit tests in Parallel using Maven Surefire Plugin
We will show you a solution to run your JUnit tests in parallel. This can be accomplished by using maven sure fire plugin. You can configure the surefire plugin to use differenct strategies.
If your tests are depending that they run in a certain order then don’t use parallelism. Because order cannot be predicted using different threads. Except when you use a locking mechanism but if you do that you might as well don’t use parallelism.
Maven Dependencies
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.memorynotfound.test.junit</groupId>
<artifactId>parallel-maven</artifactId>
<version>1.0.0-SNAPSHOT</version>
<properties>
<junit.version>4.12</junit.version>
<surefire.version>2.18</surefire.version>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>${surefire.version}</version>
<configuration>
<parallel>methods</parallel>
<threadCount>10</threadCount>
</configuration>
</plugin>
</plugins>
</build>
</project>
Available configuration
The parallel element can be configured using the following configurations.
- methods
- classes
- both (depricated since surefire 2.16) same as classesAndMethods
- suites
- suitesAndClasses
- suitesAndMethods
- classesAndMethods
- all
You can also tweak the number of threads per plan you want to use. You can add the following elements inside the configuration block: useUnlimitedThreads
, threadCount
, threadCountSuites
, threadCountClasses
, threadCountMethods
JUnit tests in Parallel
Here is the JUnit test class that we will run in parallel. Note that every method does a Thread.sleep()
of 5 seconds. So if all the tests where to run sequentially the result would be 25+ seconds in total. But we configured our tests to run in parallel so we now expect the result a little less over 5 seconds. Talk about a performance boost!
package com.memorynotfound.test;
import org.junit.Test;
public class ParallelTest {
@Test
public void one() throws InterruptedException {
Thread.sleep(1000 * 5);
}
@Test
public void two() throws InterruptedException {
Thread.sleep(1000 * 5);
}
@Test
public void three() throws InterruptedException {
Thread.sleep(1000 * 5);
}
@Test
public void four() throws InterruptedException {
Thread.sleep(1000 * 5);
}
@Test
public void five() throws InterruptedException {
Thread.sleep(1000 * 5);
}
}
As you can see this looks just like a normal JUnit test, no fancy stuff is used here. The magic however happens at maven side. The Maven Surefire Plugin will spin up multiple threads to run the JUnit tests in parallel.
Output
As you can see the total time it took to run our tests was 6.297 seconds. What a great result instead of that 25+ seconds.
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building parallel-maven 1.0.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] ...
[INFO]
[INFO] useUnlimitedThreads=false, threadCountSuites=0, threadCountClasses=0, threadCountMethods=0, parallelOptimized=true
-------------------------------------------------------
T E S T S
-------------------------------------------------------
Running com.memorynotfound.test.ParallelTest
Tests run: 5, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 25.032 sec - in com.memorynotfound.test.ParallelTest
Results :
Tests run: 5, Failures: 0, Errors: 0, Skipped: 0
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 6.297 s
[INFO] Finished at: 2014-12-18T14:52:53+01:00
[INFO] Final Memory: 9M/309M
[INFO] ------------------------------------------------------------------------