'Running JUnit5 Test in Parallel But Want To Leave Some Tests Sequential
I have a project that we have many Junit tests. We just did a large migration from JUnit4 to JUnit5. We would like to run most of the tests in parallel but have a couple that need to be ran sequentially. Is there any way to use JUnit5 and run tests both ways?
The reason I ask is that I have 4 tests that load a database into memory and I am loading data into this database. Then I run tests on that database. These are the four tests I need to run sequentially and cannot run in parallel.
Solution 1:[1]
You might take a look at @Isolated annotation
https://junit.org/junit5/docs/5.7.1/api/org.junit.jupiter.api/org/junit/jupiter/api/parallel/Isolated.html.
It guarantees that test-class won't run in parallel with other classes.
Solution 2:[2]
According to Parallel Test Execution and Single Thread Execution:
Since of maven-surefire-plugin:2.18, you can apply the JCIP annotation @net.jcip.annotations.NotThreadSafe on the Java class of JUnit test (pure test class, Suite, Parameterized, etc.) in order to execute it in single Thread instance. The Thread has name maven-surefire-plugin@NotThreadSafe and it is executed at the end of the test run.
So you could annotate your test classes with @NotThreadSafe in order to get them executed on 1 same thread (named maven-surefire-plugin@NotThreadSafe).
<dependency>
<groupId>com.github.stephenc.jcip</groupId>
<artifactId>jcip-annotations</artifactId>
<version>1.0-1</version>
<scope>test</scope>
</dependency>
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | |
| Solution 2 | Adrian |
