0
0
JUnittesting~15 mins

Parallel test configuration in JUnit - Build an Automation Script

Choose your learning style9 modes available
Configure and verify parallel test execution in JUnit
Preconditions (2)
Step 1: Create two simple test classes with multiple test methods each
Step 2: Configure JUnit to run tests in parallel using junit-platform.properties file
Step 3: Run the tests using the IDE or command line
Step 4: Observe the test execution order and timing
✅ Expected Result: Tests from different classes run in parallel, reducing total execution time compared to sequential run
Automation Requirements - JUnit 5
Assertions Needed:
Verify all tests pass successfully
Verify tests run in parallel by measuring execution time or thread names
Best Practices:
Use junit-platform.properties for parallel configuration
Keep tests independent to avoid flaky results
Use @Test annotations properly
Avoid shared mutable state between tests
Automated Solution
JUnit
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.

Common Mistakes - 4 Pitfalls
{'mistake': 'Not enabling parallel execution in junit-platform.properties', 'why_bad': 'Tests will run sequentially, so no speed benefit is gained.', 'correct_approach': "Add 'junit.jupiter.execution.parallel.enabled = true' in junit-platform.properties."}
Sharing mutable state between tests
Using @BeforeAll or @AfterAll without static modifier
Assuming parallelism without verifying thread usage
Bonus Challenge

Now add data-driven testing with 3 different inputs to one of the test methods and verify parallel execution still works.

Show Hint