Introduction
In this chapter, we will explore how to run tests in parallel using JUnit. Parallel test execution can significantly reduce the overall time required to run a large test suite, especially on multi-core processors. JUnit 5 provides built-in support for parallel test execution, allowing you to configure and manage how tests are executed concurrently.
What is Parallel Execution?
Parallel execution in JUnit allows tests to be run simultaneously in separate threads. This can be done at the class level, method level, or both, depending on your configuration. Parallel execution helps in utilizing system resources efficiently and reducing the time taken to run the entire test suite.
Configuring Parallel Execution
JUnit 5 allows you to configure parallel execution using the junit-platform.properties
file or programmatically via the LauncherDiscoveryRequest
API.
Using junit-platform.properties
You can enable and configure parallel execution by adding a junit-platform.properties
file to your test resources directory (src/test/resources
). This file allows you to specify various configuration options for parallel execution.
Example Configuration in junit-platform.properties
junit.jupiter.execution.parallel.enabled = true
junit.jupiter.execution.parallel.mode.default = concurrent
junit.jupiter.execution.parallel.mode.classes.default = concurrent
junit.jupiter.execution.parallel.enabled
: Enables or disables parallel execution.junit.jupiter.execution.parallel.mode.default
: Sets the default parallel execution mode for test methods.junit.jupiter.execution.parallel.mode.classes.default
: Sets the default parallel execution mode for test classes.
Using @Execution Annotation
JUnit 5 also provides the @Execution
annotation to specify parallel execution mode at the class or method level.
Example: Enabling Parallel Execution
Let’s create an example to demonstrate how to enable parallel execution for tests.
Step 1: Create a Class Under Test
Calculator Class
The Calculator
class will have basic arithmetic operations.
public class Calculator {
public int add(int a, int b) {
return a + b;
}
public int subtract(int a, int b) {
return a - b;
}
public int multiply(int a, int b) {
return a * b;
}
public int divide(int a, int b) {
if (b == 0) {
throw new IllegalArgumentException("Division by zero");
}
return a / b;
}
}
Step 2: Create a Test Classes
CalculatorTest
The CalculatorTest
class will contain tests for the Calculator
class with parallel execution enabled.
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.parallel.Execution;
import org.junit.jupiter.api.parallel.ExecutionMode;
import static org.junit.jupiter.api.Assertions.*;
@Execution(ExecutionMode.CONCURRENT)
public class CalculatorTest {
private Calculator calculator = new Calculator();
@Test
void testAdd() {
assertEquals(5, calculator.add(2, 3), "2 + 3 should equal 5");
}
@Test
void testSubtract() {
assertEquals(2, calculator.subtract(5, 3), "5 - 3 should equal 2");
}
@Test
void testMultiply() {
assertEquals(6, calculator.multiply(2, 3), "2 * 3 should equal 6");
}
@Test
void testDivide() {
assertEquals(2, calculator.divide(6, 3), "6 / 3 should equal 2");
}
}
Step 3: Configure Parallel Execution Globally
junit-platform.properties
Add the following configuration to the junit-platform.properties
file to enable parallel execution globally:
junit.jupiter.execution.parallel.enabled = true
junit.jupiter.execution.parallel.mode.default = concurrent
junit.jupiter.execution.parallel.mode.classes.default = concurrent
Important Points
- @Execution: Use this annotation to specify parallel execution mode at the class or method level. Available modes are
SAME_THREAD
andCONCURRENT
. - junit-platform.properties: Use this file to configure global settings for parallel execution, such as enabling parallel execution and setting the default modes for test methods and classes.
Running the Parallel Tests
To run the parallel tests, simply run the CalculatorTest
class as a JUnit test. This will execute all test methods concurrently.
Using Eclipse
- Run Tests: Right-click on the
CalculatorTest
file and selectRun As
>JUnit Test
. - View Results: The results will be displayed in the JUnit view, showing the tests executed concurrently.
Using IntelliJ IDEA
- Run Tests: Click the green run icon next to the
CalculatorTest
class and selectRun
. - View Results: The results will be displayed in the Run window, showing the tests executed concurrently.
Using VS Code
- Run Tests: Open the
CalculatorTest
file and click theRun
icon above the class declaration. - View Results: The results will be displayed in the Test Explorer, showing the tests executed concurrently.
Conclusion
JUnit parallel execution provides a powerful way to reduce the time required to run a large test suite by utilizing system resources more efficiently. By configuring parallel execution using the junit-platform.properties
file or the @Execution
annotation, you can control how tests are executed concurrently at the class or method level. This approach helps in speeding up the test execution process and ensuring that your tests run efficiently on multi-core processors.