Scrivio Interview library
ConceptIntermediate6 min read · updated 12 November 2026

How do you enable parallel test execution in JUnit 5, and how do you protect tests that share a resource?

We enable parallel execution with junit.jupiter.execution.parallel.enabled=true and choose default modes, such as concurrent classes and same-thread methods, in junit-platform.properties. @Execution(ExecutionMode.CONCURRENT) or SAME_THREAD overrides the mode for a class or method. Tests that share a resource declare it with @ResourceLock so they never run at the same time, and @Isolated runs a whole class alone. Spring's context cache keeps working across parallel tests, but the shared beans must then be thread-safe.

Why this question is asked

Parallel execution is one of the most effective ways to make a slow suite fast, and one of the fastest ways to make a stable suite flaky. Interviewers ask about it to see whether we know the switches and, more importantly, how to protect tests that share something, such as a database table, a system property or a file. In this answer, we will recap what parallel means in JUnit, turn it on step by step, control it per class and method, protect shared resources with @ResourceLock and @Isolated, and look at what changes for Spring tests.

1A Quick Recap of Parallel Execution in JUnit

By default JUnit Jupiter runs all tests one after another on a single thread. With parallel execution, it uses a ForkJoinPool, a pool of worker threads, to run several tests at the same time. Two separate choices are involved: whether different test classes run at the same time, and whether methods within one class run at the same time. JUnit lets us set each independently, which matters because methods in one class often share fields and setup, while different classes are more often independent.

2Turning It On

Parallel execution is off until we set a configuration parameter, usually in src/test/resources/junit-platform.properties. Then we choose the default modes. A safe starting point is to run classes concurrently and methods within a class on the same thread.

Properties
# src/test/resources/junit-platform.properties
junit.jupiter.execution.parallel.enabled = true
# methods inside a class: one after another
junit.jupiter.execution.parallel.mode.default = same_thread
# different classes: at the same time
junit.jupiter.execution.parallel.mode.classes.default = concurrent
# how many threads: dynamic = number of cores x factor
junit.jupiter.execution.parallel.config.strategy = dynamic
junit.jupiter.execution.parallel.config.dynamic.factor = 1
The two default modes combined
ClassesMethodsEffect
same_threadsame_threadnothing runs in parallel
concurrentsame_threadclasses in parallel, methods in order: safe start
same_threadconcurrentmethods of one class in parallel, classes in order
concurrentconcurrenteverything may run at once: fastest, most risk

Notice that enabling alone changes nothing visible if both modes stay same_thread; the mode settings decide what actually runs in parallel. With the settings above on an 8-core machine, up to 8 classes run at once. On a suite of mostly independent unit tests this often cuts time by more than half.

3Controlling Individual Classes and Methods

Defaults rarely fit every test. @Execution overrides them for one class or method. A class whose methods are truly independent, such as pure calculation tests, can opt into CONCURRENT, and a class that is not safe can be pinned to SAME_THREAD.

Java
@Execution(ExecutionMode.CONCURRENT) // these methods share nothing
class TaxCalculatorTest {
@Test void ukRate() { assertEquals(new BigDecimal("20.00"), calc.tax("GB", HUNDRED)); }
@Test void deRate() { assertEquals(new BigDecimal("19.00"), calc.tax("DE", HUNDRED)); }
}
@Execution(ExecutionMode.SAME_THREAD) // methods share a mutable fixture
class ImportPipelineTest { }

4Protecting Shared Resources

Some tests touch something outside themselves: system properties, a shared database table, a file, a fixed network port. Two such tests running at once corrupt each other. @ResourceLock names the resource, and JUnit guarantees that tests holding a READ_WRITE lock on the same name never run at the same time, while tests holding only READ locks may run together. JUnit ships constants for common ones, such as Resources.SYSTEM_PROPERTIES, Resources.LOCALE and Resources.TIME_ZONE, and we can invent our own names.

Java
class ProxyConfigTest {
@Test
@ResourceLock(value = Resources.SYSTEM_PROPERTIES, mode = ResourceAccessMode.READ_WRITE)
void readsProxyFromProperties() {
System.setProperty("https.proxyHost", "proxy.local");
try { assertEquals("proxy.local", config.proxyHost()); }
finally { System.clearProperty("https.proxyHost"); }
}
}
@ResourceLock("orders-table") // our own name: every class touching the table
class OrderReportTest { }
@Isolated // runs alone: nothing else in parallel
class DefaultTimeZoneTest { }
Step throughCan two tests run at the same time?
  1. Is parallel execution enabled?
    YesCheck modesNoNo, sequential
  2. Is either test or class @Isolated?
    YesNo, run aloneNoCheck locks
  3. Same @ResourceLock name, any READ_WRITE?
    YesNo, one waitsNoYes, if modes allow

3 steps in order. Play them, or step through them one at a time.

@Isolated is the blunt tool: while the class runs, no other test runs at all. It is right for tests that change something global and hard to name, but each isolated class removes parallelism for its duration, so we use it sparingly.

5What Changes for Spring Tests

The Spring TestContext framework's context cache works with parallel execution: classes with the same configuration still share one application context, and Spring builds it once even when two classes ask at the same moment. But sharing a context between threads means the beans are now used by two tests at once. Singleton beans with mutable state, such as an in-memory fake that records calls, a cache, or a mock configured differently by two classes, will interfere. So beans used in tests must be thread-safe or stateless, mocks created with @MockitoBean should not be shared between concurrently running classes with different stubbing, and database tests need separate data or locks.

The database is the usual limit. Two transactional tests rolling back on the same schema are fine if they use different rows, but tests that truncate tables or count rows need @ResourceLock on that table, a schema per worker, or a separate container per fork.

6Follow-up questions

Does Maven Surefire's forking do the same thing?

Not quite. Surefire's forkCount runs tests in several JVMs, which isolates static state and system properties but does not share a Spring context between forks, so each fork builds its own. JUnit's parallel execution uses threads in one JVM, which shares the context cache but also shares statics. Many teams combine a small number of forks with JUnit parallelism inside each.

How do you roll parallel execution out on an existing suite?

I would enable it with concurrent classes and same-thread methods, run the suite many times in CI, and look for new failures. Each failing class either gets fixed to be independent, gets a @ResourceLock for what it shares, or is temporarily marked SAME_THREAD or @Isolated with a ticket. Measuring the time before and after shows whether the effort pays.

What is the difference between @ResourceLock READ and READ_WRITE?

READ means the test only reads the resource, so several READ holders may run together. READ_WRITE means it changes it, so it excludes every other test that locks the same resource, whether READ or READ_WRITE.

7Conclusion

In this answer, we enabled parallel execution with junit.jupiter.execution.parallel.enabled=true and chose separate default modes for classes and methods in junit-platform.properties. @Execution(CONCURRENT or SAME_THREAD) overrides them per class or method, @ResourceLock keeps tests that share a resource such as system properties or a table from overlapping, and @Isolated runs a class alone. Spring's context cache still works across parallel tests, but shared beans and database data must be safe for concurrent use.

What to say in the interviewAbout 80 seconds aloud

Open with

I turn it on in src/test/resources/junit-platform.properties with junit.jupiter.execution.parallel.enabled=true, and then I choose the default modes, because enabling alone runs nothing in parallel. I usually start with mode.classes.default set to concurrent and mode.default set to same_thread, so different classes run at the same time but methods within a class run in order, since methods often share fields.

Then, if they want more

The thread count comes from the dynamic strategy, cores times a factor. For individual classes or methods I can override with @Execution(ExecutionMode.CONCURRENT) or SAME_THREAD.

To protect tests that share something, like system properties, the default time zone or a database table, I use @ResourceLock with a name, either JUnit's constants such as Resources.SYSTEM_PROPERTIES or my own like orders-table, and a mode; tests with READ_WRITE on the same name never run together, while READ holders can.

For tests that change something global and hard to name, @Isolated runs the class with nothing else in parallel, but I use it sparingly.

In Spring tests, the context cache still works across parallel classes, but then the beans are shared between threads, so they must be thread-safe or stateless, and database tests need separate data, locks or a schema per worker.

About this answer

Last updated
12 November 2026
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Official documentation
JUnit user guide
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "How do you enable parallel test execution in JUnit 5, and how do you protect tests that...": https://scrivio-interview-library.netlify.app/java/junit/1ca5911277/