Running tests in parallel saves time by checking many things at once instead of one by one.
Parallel test configuration in JUnit
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.parallel.Execution; import org.junit.jupiter.api.parallel.ExecutionMode; @Execution(ExecutionMode.CONCURRENT) public class MyParallelTests { @Test void testOne() { // test code here } @Test void testTwo() { // test code here } }
Use @Execution(ExecutionMode.CONCURRENT) on the test class or method to enable parallel execution.
JUnit 5 supports parallel execution natively with this annotation and configuration.
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.parallel.Execution; import org.junit.jupiter.api.parallel.ExecutionMode; @Execution(ExecutionMode.CONCURRENT) public class ParallelTests { @Test void testA() { // test A code } @Test void testB() { // test B code } }
testX runs in parallel; testY runs normally.import org.junit.jupiter.api.Test; import org.junit.jupiter.api.parallel.Execution; import org.junit.jupiter.api.parallel.ExecutionMode; public class MixedTests { @Test @Execution(ExecutionMode.CONCURRENT) void testX() { // test X runs in parallel } @Test @Execution(ExecutionMode.SAME_THREAD) void testY() { // test Y runs sequentially } }
This test class runs two tests at the same time. Each test waits 1 second. Running in parallel means total time is about 1 second, not 2.
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.parallel.Execution; import org.junit.jupiter.api.parallel.ExecutionMode; @Execution(ExecutionMode.CONCURRENT) public class SampleParallelTest { @Test void testOne() throws InterruptedException { System.out.println("Start testOne"); Thread.sleep(1000); System.out.println("End testOne"); } @Test void testTwo() throws InterruptedException { System.out.println("Start testTwo"); Thread.sleep(1000); System.out.println("End testTwo"); } }
Make sure tests do not share or change the same data to avoid conflicts when running in parallel.
You can configure parallelism level in JUnit platform properties if needed.
Parallel tests help speed but can make debugging harder if tests interfere.
Parallel test configuration runs tests at the same time to save time.
Use @Execution(ExecutionMode.CONCURRENT) to enable parallel tests in JUnit.
Ensure tests are independent to avoid errors when running in parallel.
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
