0
0
JunitHow-ToBeginner ยท 3 min read

How to Run Tests in Parallel with JUnit 5

In JUnit 5, you can run tests in parallel by configuring the junit.jupiter.execution.parallel.enabled property to true in the junit-platform.properties file or programmatically. This enables parallel execution of test classes and methods, improving test speed without extra dependencies.
๐Ÿ“

Syntax

To enable parallel test execution in JUnit 5, set the following properties in junit-platform.properties:

  • junit.jupiter.execution.parallel.enabled=true - turns on parallel execution.
  • junit.jupiter.execution.parallel.mode.default=concurrent - runs test methods in parallel.
  • junit.jupiter.execution.parallel.mode.classes.default=concurrent - runs test classes in parallel.

You can also configure parallelism level and strategy with other properties.

properties
junit.jupiter.execution.parallel.enabled = true
junit.jupiter.execution.parallel.mode.default = concurrent
junit.jupiter.execution.parallel.mode.classes.default = concurrent
๐Ÿ’ป

Example

This example shows two test classes with multiple test methods running in parallel using JUnit 5 configuration.

java
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertTrue;

public class ParallelTest1 {
    @Test
    void testA() throws InterruptedException {
        Thread.sleep(500);
        assertTrue(true);
    }

    @Test
    void testB() throws InterruptedException {
        Thread.sleep(500);
        assertTrue(true);
    }
}

class ParallelTest2 {
    @Test
    void testC() throws InterruptedException {
        Thread.sleep(500);
        assertTrue(true);
    }

    @Test
    void testD() throws InterruptedException {
        Thread.sleep(500);
        assertTrue(true);
    }
}
Output
All tests pass, and total execution time is about 1 second instead of 2 seconds, showing parallel execution.
โš ๏ธ

Common Pitfalls

Common mistakes when running JUnit tests in parallel include:

  • Not enabling parallel execution in junit-platform.properties.
  • Sharing mutable state between tests causing flaky failures.
  • Using thread-unsafe resources without synchronization.
  • Expecting parallelism in JUnit 4, which lacks built-in support.

Always design tests to be independent and thread-safe for parallel runs.

properties
/* Wrong: No parallel enabled, tests run sequentially */
// junit-platform.properties
junit.jupiter.execution.parallel.enabled = false

/* Right: Enable parallel execution */
junit.jupiter.execution.parallel.enabled = true
junit.jupiter.execution.parallel.mode.default = concurrent
junit.jupiter.execution.parallel.mode.classes.default = concurrent
๐Ÿ“Š

Quick Reference

PropertyDescriptionExample Value
junit.jupiter.execution.parallel.enabledEnable parallel test executiontrue
junit.jupiter.execution.parallel.mode.defaultParallel mode for test methodsconcurrent
junit.jupiter.execution.parallel.mode.classes.defaultParallel mode for test classesconcurrent
junit.jupiter.execution.parallel.config.strategyParallelism strategy (fixed or dynamic)fixed
junit.jupiter.execution.parallel.config.fixed.parallelismNumber of threads for fixed strategy4
โœ…

Key Takeaways

Enable parallel execution by setting junit.jupiter.execution.parallel.enabled to true.
Configure parallel mode for both test methods and classes to run concurrently.
Ensure tests are independent and thread-safe to avoid flaky failures.
JUnit 5 supports parallel execution natively; JUnit 4 does not.
Adjust parallelism strategy and thread count for optimal performance.