In this article, we look at how to execute tests in parallel using JUnit5. First, we will have to add one line of configuration to our project to enable the Parallel Execution feature. Then, after enabling Parallel Execution, we look at how to use this feature. The last example will be about synchronizing tests that share a common resource while still trying to run other tests in parallel.
To enable parallel execution we need to create a file called junit-platform.properties inside the src/test/resources folder. Inside the file, you need to add the following line:
The line above will enable the basic functionality and allow you to run tests in parallel. However, there a more settings to customize the parallel execution of unit tests.
customize the parallel execution of unit tests
If you want all your tests to run in parallel by default, you must add the following line:
JUnit includes two strategies that allow you to change how many threads will be used to run tests in parallel.
- dynamic: Computes the number of threads it can use. The default is 1. The default will use all the threads available. When you set it to .5, only half the amount of threads will be used.
- fixed: the value will equal the number of threads used for running tests in parallel.
For a dynamic strategy, add the following lines:
For a fixed strategy, add the following lines:
If you only added
junit.jupiter.execution.parallel.enabled=true to the junit-platform properties file and didn't change the default parallel mode.
Then, you need to add the
@Execution(CONCURRENT) annotation to the test class containing the unit tests you want to run in parallel.
In the following example, I have a test class annotated with
@Execution(CONCURRENT) to run the unit tests in parallel.
The total runtime of this class will now be 3 seconds instead of 6 seconds.
Unit tests shouldn't be dependent on each other. There are, however, test cases where unit tests share a recourse. For example, running tests in parallel that read and write
to a shared resource can be flaky and unexpectedly fail. To prevent having test fails because they run in parallel, we use the
@ResourceLock(value = "KeyName, mode = READ) annotation.
mode inside the @ResourceLock tells JUnit what action the unit test performs on the shared resource. For example, READ actions can
run in parallel, While READ_WRITE will run concurrently. The
value is a key/name for the shared resource.
READ unit tests will run in parallel with other READ tests while READ_WRITE will run alone/in the same thread
In the following example, we have three tests that use a shared database resource with the key name “DB”. The tests annotated with
@ResourceLock(value = "DB", mode = READ)
will run in parallel. The method annotated with
@ResourceLock(value = "DB", mode = READ_WRITE) will run in the same thread.
The total runtime of this class is 4 seconds.
In this article, we looked at how to execute unit tests in parallel using JUnit5. First, we enabled the option to run tests in parallel. We covered different kinds of configurations that are available for parallel execution. We created tests that run in parallel and saw how to coordinate tests that share a resource. Please see the official documentation if you want to know more about JUnit5
More about testing in Java: