Parallel test configuration in JUnit - Build an Automation Script
import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.assertTrue; public class ParallelTest1 { @Test void testOne() throws InterruptedException { System.out.println("ParallelTest1 - testOne - Thread: " + Thread.currentThread().getName()); Thread.sleep(1000); // simulate work assertTrue(true); } @Test void testTwo() throws InterruptedException { System.out.println("ParallelTest1 - testTwo - Thread: " + Thread.currentThread().getName()); Thread.sleep(1000); assertTrue(true); } } public class ParallelTest2 { @Test void testThree() throws InterruptedException { System.out.println("ParallelTest2 - testThree - Thread: " + Thread.currentThread().getName()); Thread.sleep(1000); assertTrue(true); } @Test void testFour() throws InterruptedException { System.out.println("ParallelTest2 - testFour - Thread: " + Thread.currentThread().getName()); Thread.sleep(1000); assertTrue(true); } } // junit-platform.properties file content (place in src/test/resources): junit.jupiter.execution.parallel.enabled = true junit.jupiter.execution.parallel.mode.default = concurrent junit.jupiter.execution.parallel.config.strategy = fixed junit.jupiter.execution.parallel.config.fixed.parallelism = 2 // To run tests, use your IDE or command line: mvn test or gradle test // Observe console output for thread names and timing to confirm parallel execution.
The two test classes ParallelTest1 and ParallelTest2 each have two test methods. Each test prints the current thread name and sleeps for 1 second to simulate work.
The junit-platform.properties file enables parallel execution and sets the parallelism to 2 threads.
When running the tests, you will see that tests from different classes run on different threads concurrently, reducing total test time.
This setup uses JUnit 5's built-in parallel execution feature configured via properties, which is the recommended way to enable parallel tests.
Assertions verify tests pass, and printing thread names helps confirm parallelism.
Now add data-driven testing with 3 different inputs to one of the test methods and verify parallel execution still works.
Practice
Solution
Step 1: Understand parallel test execution
Parallel test execution means running tests simultaneously instead of one by one.Step 2: Identify the benefit
Running tests at the same time saves overall testing time.Final Answer:
To run multiple tests at the same time and save testing time -> Option CQuick Check:
Parallel tests = faster testing [OK]
- Thinking parallel means disabling tests
- Confusing parallel with sequential execution
- Assuming parallel fixes test failures
Solution
Step 1: Recall JUnit 5 parallel test annotation
JUnit 5 uses @Execution with ExecutionMode.CONCURRENT to run tests in parallel.Step 2: Check other options
@RunWith is from JUnit 4, and @ParallelTest or @ConcurrentExecution do not exist in JUnit 5.Final Answer:
@Execution(ExecutionMode.CONCURRENT) -> Option AQuick Check:
@Execution(CONCURRENT) enables parallel tests [OK]
- Using JUnit 4 annotations in JUnit 5
- Assuming @ParallelTest exists
- Confusing annotation names
import org.junit.jupiter.api.*;
import org.junit.jupiter.api.parallel.Execution;
import org.junit.jupiter.api.parallel.ExecutionMode;
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
@Execution(ExecutionMode.CONCURRENT)
public class SampleTest {
@Test
void testA() throws InterruptedException {
Thread.sleep(100);
System.out.println("Test A completed");
}
@Test
void testB() {
System.out.println("Test B completed");
}
}Solution
Step 1: Understand @Execution(ExecutionMode.CONCURRENT)
This annotation runs tests in parallel, so testA and testB start at the same time.Step 2: Analyze test behavior
testA sleeps 100ms before printing, testB prints immediately. So testB likely prints first, but order can vary.Final Answer:
Test B completed Test A completed (order may vary due to concurrency) -> Option BQuick Check:
Parallel tests print order varies [OK]
- Assuming tests run sequentially despite @Execution
- Expecting fixed print order
- Thinking @Execution causes compile errors
Solution
Step 1: Identify cause of intermittent failures
Shared mutable data causes race conditions when tests run in parallel.Step 2: Apply best practice for parallel tests
Tests must be independent and avoid shared mutable state to run safely in parallel.Final Answer:
Make tests independent by avoiding shared mutable state -> Option AQuick Check:
Independent tests avoid parallel conflicts [OK]
- Just removing parallel execution instead of fixing tests
- Adding sleeps does not fix race conditions
- Disabling tests hides the problem
Solution
Step 1: Understand JUnit 5 parallel config properties
Propertyjunit.jupiter.execution.parallel.mode.defaultcontrols method-level parallelism;junit.jupiter.execution.parallel.mode.classes.defaultcontrols class-level parallelism.Step 2: Configure methods parallel, classes sequential
Setting methods to concurrent and classes to same_thread runs tests in parallel within classes but not across classes.Final Answer:
Set junit.jupiter.execution.parallel.mode.default to concurrent and junit.jupiter.execution.parallel.mode.classes.default to same_thread -> Option DQuick Check:
Methods parallel + classes sequential = parallel in class only [OK]
- Assuming @Execution on methods controls class-level parallelism
- Disabling parallel execution entirely
- Using SAME_THREAD on classes disables all parallelism
Ready to prove it?
Pass the test and this pattern moves unknown
