Reputation: 336
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.
Upvotes: 6
Views: 11766
Reputation: 71
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.
Upvotes: 7
Reputation: 3711
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>
Upvotes: 1
Reputation: 6249
You most likely want to use @ResourceLock
annotation on tests
https://junit.org/junit5/docs/snapshot/user-guide/#writing-tests-parallel-execution-synchronization
Upvotes: 4