How to execute junit test cases parallely using maven surefire plugin?

Junit test parallel execution can bring down the test execution time considerably. The only condition is that your test cases must be independent. The state shall not be shared across test cases. Today, I shall share a maven project with parallel test execution configured using maven surefire plugin.

The complete source code can be found in Github.

Surefire plugin configuration

Sequential test execution time

-------------------------------------------------------
 T E S T S
-------------------------------------------------------
Running com.ourownjava.junit.TestUnitUnderTest2
TestUnitUnderTest2.shouldReturnMethod1()Sat Nov 29 19:04:19 EST 2014
TestUnitUnderTest2.shouldReturnMethod2()Sat Nov 29 19:04:21 EST 2014
TestUnitUnderTest2.shouldReturnMethod3()Sat Nov 29 19:04:23 EST 2014
Tests run: 3, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 6.792 sec
Running com.ourownjava.junit.TestUnitUnderTest1
TestUnitUnderTest1.shouldReturnMethod1()Sat Nov 29 19:04:25 EST 2014
TestUnitUnderTest1.shouldReturnMethod2()Sat Nov 29 19:04:27 EST 2014
TestUnitUnderTest1.shouldReturnMethod3()Sat Nov 29 19:04:29 EST 2014
Tests run: 3, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 6.008 sec

Results :

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

[INFO] 
[INFO] --- maven-jar-plugin:2.2:jar (default-jar) @ junit-parallel-execution ---
[INFO] Building jar: /home/thosan/java/gitrepo/junit-parallel-execution/junit-parallel-execution/target/junit-parallel-execution-0.0.1-SNAPSHOT.jar
[INFO] 
[INFO] --- maven-install-plugin:2.3:install (default-install) @ junit-parallel-execution ---
[INFO] Installing /home/thosan/java/gitrepo/junit-parallel-execution/junit-parallel-execution/target/junit-parallel-execution-0.0.1-SNAPSHOT.jar to /home/thosan/.m2/repository/com/ourownjava/junit-parallel-execution/0.0.1-SNAPSHOT/junit-parallel-execution-0.0.1-SNAPSHOT.jar
[INFO] Installing /home/thosan/java/gitrepo/junit-parallel-execution/junit-parallel-execution/pom.xml to /home/thosan/.m2/repository/com/ourownjava/junit-parallel-execution/0.0.1-SNAPSHOT/junit-parallel-execution-0.0.1-SNAPSHOT.pom
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 17.908s
[INFO] Finished at: Sat Nov 29 19:04:31 EST 2014
[INFO] Final Memory: 14M/172M
[INFO] ------------------------------------------------------------------------

Test execution time for parallel test execution

-------------------------------------------------------
 T E S T S
-------------------------------------------------------
Running com.ourownjava.junit.TestUnitUnderTest2
TestUnitUnderTest2.shouldReturnMethod1()Sat Nov 29 19:02:49 EST 2014
TestUnitUnderTest2.shouldReturnMethod3()Sat Nov 29 19:02:49 EST 2014
TestUnitUnderTest2.shouldReturnMethod2()Sat Nov 29 19:02:49 EST 2014
Running com.ourownjava.junit.TestUnitUnderTest1
TestUnitUnderTest1.shouldReturnMethod1()Sat Nov 29 19:02:49 EST 2014
TestUnitUnderTest1.shouldReturnMethod3()Sat Nov 29 19:02:49 EST 2014
TestUnitUnderTest1.shouldReturnMethod2()Sat Nov 29 19:02:49 EST 2014
Tests run: 3, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 6.148 sec - in com.ourownjava.junit.TestUnitUnderTest1
Tests run: 3, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 6.284 sec - in com.ourownjava.junit.TestUnitUnderTest2

Results :


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

[INFO] 
[INFO] --- maven-jar-plugin:2.2:jar (default-jar) @ junit-parallel-execution ---
[INFO] Building jar: /home/thosan/java/gitrepo/junit-parallel-execution/junit-parallel-execution/target/junit-parallel-execution-0.0.1-SNAPSHOT.jar
[INFO] 
[INFO] --- maven-install-plugin:2.3:install (default-install) @ junit-parallel-execution ---
[INFO] Installing /home/thosan/java/gitrepo/junit-parallel-execution/junit-parallel-execution/target/junit-parallel-execution-0.0.1-SNAPSHOT.jar to /home/thosan/.m2/repository/com/ourownjava/junit-parallel-execution/0.0.1-SNAPSHOT/junit-parallel-execution-0.0.1-SNAPSHOT.jar
[INFO] Installing /home/thosan/java/gitrepo/junit-parallel-execution/junit-parallel-execution/pom.xml to /home/thosan/.m2/repository/com/ourownjava/junit-parallel-execution/0.0.1-SNAPSHOT/junit-parallel-execution-0.0.1-SNAPSHOT.pom
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 8.548s
[INFO] Finished at: Sat Nov 29 19:02:52 EST 2014
[INFO] Final Memory: 14M/171M
[INFO] ------------------------------------------------------------------------

Leave a comment

Your email address will not be published. Required fields are marked *