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=""






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

Note: The parallel functionality is only available with junit 4.7 and onwards.

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 {

    public void one() throws InterruptedException {
        Thread.sleep(1000 * 5);

    public void two() throws InterruptedException {
        Thread.sleep(1000 * 5);

    public void three() throws InterruptedException {
        Thread.sleep(1000 * 5);

    public void four() throws InterruptedException {
        Thread.sleep(1000 * 5);

    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.


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] Building parallel-maven 1.0.0-SNAPSHOT
[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] ------------------------------------------------------------------------
[INFO] Total time: 6.297 s
[INFO] Finished at: 2014-12-18T14:52:53+01:00
[INFO] Final Memory: 9M/309M
[INFO] ------------------------------------------------------------------------


You may also like...