Run REST Assured Tests in Parallel With JUnit
Run API tests concurrently with JUnit's parallel execution, configured in junit-platform.properties and opted into with @Execution(CONCURRENT), and follow the rules that make REST Assured safe in parallel: no static configuration, immutable specifications and no shared test data.
API tests spend most of their time waiting: for the network, the server, the database. A suite of a thousand requests at 200 milliseconds each takes over three minutes, almost all of it idle. Running tests in parallel turns that waiting into throughput. But parallel tests break in ways sequential ones never do: one test sets a static REST Assured default while another sends a request, and the second test fails for a reason that’s nowhere in its code. In this guide, you’ll run a class of API tests on four threads, measure the gain, and see the rule that keeps parallel tests safe break in front of you.
Level: Advanced · Time: 20 minutes · Part of: Module 12: Real Infrastructure with Testcontainers, section 2
What You’ll Build
You’ll build a test class called ParallelExecutionTest with eight independent reads against a slow reference data API, configured to run on up to four threads, and you’ll compare its time with a sequential run.
On four threads, the class reports which threads it used:
1
ParallelExecutionTest ran on 4 thread(s): [ForkJoinPool-1-worker-1, ForkJoinPool-1-worker-4, ForkJoinPool-1-worker-2, ForkJoinPool-1-worker-3]
When you’re done, Maven reports eight passing tests, in less than half the sequential time:
1
2
[INFO] Tests run: 8, Failures: 0, Errors: 0, Skipped: 0
[INFO] BUILD SUCCESS
In this guide, you will:
- Configure parallel execution.
- Write tests that can run together.
- Measure the difference.
What You Need
- About 20 minutes
- JDK 26 or later
- Maven 3.9 or later
- An IDE (IntelliJ IDEA, VS Code or Eclipse)
- No Docker: this section’s API runs in the test’s JVM.
- Knowledge: you can build and share a request specification, as taught in Build Reusable Request Specifications in REST Assured, and you know why tests should be independent, from Test API Workflows With REST Assured.
How to Complete This Guide
You can work through this guide in one of two ways:
- Build it step by step: create
ParallelExecutionTestin a newsection_02package next to the class from the previous section, and follow every heading below. - Skip ahead: open the finished class (
ParallelExecutionTest.java) and read along. You can run it at any time with the command in Run the Tests.
Get the code: if you haven’t cloned the examples repository yet, follow the steps in Test With Real OAuth 2.0 Tokens From Keycloak and Testcontainers.
Out of scope: this guide doesn’t cover running test classes in separate JVMs with Surefire’s forkCount, or making a write-heavy suite parallel; the tests here only read.
Understand Parallel Execution
The problem. API tests are slow because they wait, not because they compute. Run one after another, the waits add up. Run together, they overlap, but any state the tests share, such as REST Assured’s static defaults, becomes a race.
The concept. JUnit Jupiter can run tests on several threads. Two settings decide what runs concurrently:
- Configuration parameters, usually in
src/test/resources/junit-platform.properties, turn parallel execution on and choose the default mode and the number of threads. @Execution(..)on a class or method overrides the default:CONCURRENTruns its tests in parallel,SAME_THREADkeeps them with their parent.
The rules for parallel REST Assured tests:
| Rule | Because |
|---|---|
| No static configuration | RestAssured.baseURI, filters(..), config and the rest are shared by every thread, and reset() clears them for all |
| Immutable, shared specifications | A built RequestSpecification can be shared; the request given(spec) builds is each test’s own |
| No shared mutable test data | Two tests that create or change the same record collide |
| Thread-safe collections in the test | A HashSet shared between threads loses updates |
| An API that handles concurrent requests | The API under test sees the load of all threads at once |
When to use each. Opt classes in one at a time, after checking that each follows the rules. Once a whole suite is safe, make concurrent the default.
Share nothing that can change, and parallel tests are as reliable as sequential ones.
Meet the API Under Test
ReferenceDataApi serves read-only reference data, with a 200 ms delay on every request, as a slow back end would:
| Request | Answer |
|---|---|
GET /api/v1/currencies | Four currencies: EUR, GBP, JPY and KWD |
GET /api/v1/currencies/{code} | One currency with its minorUnits, or 404 |
GET /api/v1/holidays/{country} | The bank holidays of NL or EU (TARGET2), as dates |
The currencies differ in their minor units: two for the euro, none for the yen, three for the Kuwaiti dinar:
1
2
[{"code":"EUR","name":"Euro","minorUnits":2},{"code":"GBP","name":"Pound sterling","minorUnits":2},
{"code":"JPY","name":"Yen","minorUnits":0},{"code":"KWD","name":"Kuwaiti dinar","minorUnits":3}]
Your tests check these values:
- The number of currencies, and each currency’s
minorUnits. - Holidays such as King’s Day (
2026-04-27), Liberation Day (2026-05-05) and Christmas (2026-12-25). - The threads the tests ran on, and the time the class took.
Configure Parallel Execution
Turn It On for the Module
Goal: enable parallel execution, but keep every test sequential unless its class opts in.
The module’s src/test/resources/junit-platform.properties contains:
1
2
3
4
5
6
7
# Parallel execution (Module 12, section 2). Enabled for this module only, and opt-in per class:
# everything runs in the same thread unless a class or method says @Execution(CONCURRENT).
junit.jupiter.execution.parallel.enabled=true
junit.jupiter.execution.parallel.mode.default=same_thread
junit.jupiter.execution.parallel.mode.classes.default=same_thread
junit.jupiter.execution.parallel.config.strategy=fixed
junit.jupiter.execution.parallel.config.fixed.parallelism=4
What’s happening:
enabled=trueturns the feature on, for this module only.mode.default=same_threadandmode.classes.default=same_threadkeep every test and class sequential by default.strategy=fixedwithparallelism=4uses up to four threads.
Checkpoint: the module’s other test classes, such as the Keycloak tests, still run one test at a time. Nothing has changed yet.
Opt in class by class. With
mode.default=concurrent, every class in the module would run in parallel at once, and hidden shared state would fail at random. The opt-in set-up lets you check each class against the rules first.
Write Tests That Can Run Together
Set Up the Test Class
Goal: opt the class in, and share one immutable specification between threads.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package com.javanibble.module_12.section_02;
import static io.restassured.RestAssured.given;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.hasItem;
import static org.hamcrest.Matchers.hasSize;
import com.javanibble.reference.ReferenceDataApi;
import io.restassured.builder.RequestSpecBuilder;
import io.restassured.specification.RequestSpecification;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.parallel.Execution;
import org.junit.jupiter.api.parallel.ExecutionMode;
@Execution(ExecutionMode.CONCURRENT)
class ParallelExecutionTest {
private static ReferenceDataApi api;
private static RequestSpecification referenceData;
private static final Set<String> THREADS = ConcurrentHashMap.newKeySet();
@BeforeAll
static void startApi() {
api = ReferenceDataApi.start();
referenceData = new RequestSpecBuilder()
.setBaseUri(api.baseUri()).setPort(api.port()).setBasePath("/api/v1")
.build();
}
@AfterAll
static void stopApiAndReport() {
api.close();
System.out.println("ParallelExecutionTest ran on " + THREADS.size() + " thread(s): " + THREADS);
}
private static RequestSpecification request() {
THREADS.add(Thread.currentThread().getName());
return given(referenceData);
}
}
What’s happening:
@Execution(CONCURRENT)opts the class in: its tests run on the pool’s threads.referenceDatais built once in@BeforeAlland shared by every test. A built specification is immutable, so threads can share it.given(referenceData)creates a new request for each test, so no two threads change the same request.THREADSrecords which threads ran the tests. It’s shared mutable state, so it’s a concurrent set, not aHashSet.
Checkpoint: the class compiles. There are no tests yet, so there’s nothing to run until the next step.
Add Independent Reads
Goal: add eight tests that don’t depend on each other and don’t change anything.
Add these tests to the class:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
@Test
void allCurrencies() {
request().when().get("/currencies").then().statusCode(200).body("$", hasSize(4));
}
@Test
void euro() {
request().when().get("/currencies/{code}", "EUR").then().body("minorUnits", equalTo(2));
}
@Test
void yenHasNoMinorUnits() {
request().when().get("/currencies/{code}", "JPY").then().body("minorUnits", equalTo(0));
}
@Test
void dinarHasThree() {
request().when().get("/currencies/{code}", "KWD").then().body("minorUnits", equalTo(3));
}
@Test
void unknownCurrency() {
request().when().get("/currencies/{code}", "XXX").then().statusCode(404);
}
@Test
void target2Holidays() {
request().when().get("/holidays/{country}", "EU").then().body("dates", hasItem("2026-12-25"));
}
@Test
void kingsDay() {
request().when().get("/holidays/{country}", "NL").then().body("dates", hasItem("2026-04-27"));
}
@Test
void liberationDay() {
request().when().get("/holidays/{country}", "NL").then().body("dates", hasItem("2026-05-05"));
}
What’s happening:
- Each test is one read of about 200 ms. None depends on another, and none changes anything, so any order and any overlap gives the same result.
request()records the thread before every request.
Checkpoint: run ParallelExecutionTest from your IDE. All eight tests pass, and the console reports the threads they ran on.
Measure the Difference
Compare Parallel and Sequential Runs
Goal: see what the four threads saved.
Run the class as it is, then once with parallel execution turned off:
1
2
mvn test -pl module-12 -Dtest=ParallelExecutionTest
mvn test -pl module-12 -Dtest=ParallelExecutionTest -Djunit.jupiter.execution.parallel.enabled=false
The two runs reported:
1
2
3
4
5
ParallelExecutionTest ran on 4 thread(s): [ForkJoinPool-1-worker-1, ForkJoinPool-1-worker-4, ForkJoinPool-1-worker-2, ForkJoinPool-1-worker-3]
Tests run: 8, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.869 s
ParallelExecutionTest ran on 1 thread(s): [main]
Tests run: 8, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 2.139 s
What’s happening:
- On four threads, the eight 200 ms requests ran in two waves of four, in under a second.
- Turned off, they ran one after another on
main, in over two seconds. - Both times include starting the API. The gain grows with the number of slow requests, up to what the API under test can handle.
Checkpoint: your parallel run takes less than half the time of your sequential run.
REST Assured 6.2 fixed races inside its static configuration, so reading static defaults is safer than it was. That doesn’t make setting them safe: a value one test sets is still every test’s value, as the next section shows.
Run the Tests
Run the following command from the repository root:
1
mvn test -pl module-12 -Dtest=ParallelExecutionTest
You should see a result similar to the following:
1
2
3
4
5
6
ParallelExecutionTest ran on 4 thread(s): [ForkJoinPool-1-worker-1, ForkJoinPool-1-worker-4, ForkJoinPool-1-worker-2, ForkJoinPool-1-worker-3]
[INFO] Tests run: 8, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.944 s -- in com.javanibble.module_12.section_02.ParallelExecutionTest
[INFO]
[INFO] Tests run: 8, Failures: 0, Errors: 0, Skipped: 0
[INFO]
[INFO] BUILD SUCCESS
Make a Test Fail
Break the first rule. Add @TestMethodOrder(MethodOrderer.OrderAnnotation.class) to the class, so this test starts first, and add it, with imports for io.restassured.RestAssured, org.junit.jupiter.api.MethodOrderer, org.junit.jupiter.api.Order and org.junit.jupiter.api.TestMethodOrder:
1
2
3
4
5
6
7
8
9
10
11
12
@Test
@Order(1)
void aTestThatSetsAStaticFilter() throws InterruptedException {
RestAssured.filters((request, responseSpec, context) -> {
throw new AssertionError("a filter from another test: " + request.getURI());
});
try {
Thread.sleep(1000); // other tests run meanwhile
} finally {
RestAssured.reset();
}
}
Run the command again. You should see a result similar to the following:
1
2
3
4
5
[ERROR] Failures:
[ERROR] ParallelExecutionTest.allCurrencies:70->lambda$aTestThatSetsAStaticFilter$0:111 a filter from another test: http://localhost:61698/api/v1/currencies
[ERROR] ParallelExecutionTest.dinarHasThree:85->lambda$aTestThatSetsAStaticFilter$0:111 a filter from another test: http://localhost:61698/api/v1/currencies/KWD
...
[ERROR] Tests run: 9, Failures: 8, Errors: 0, Skipped: 0
Every other test failed, in three runs out of three, though none of them uses a static default: given(referenceData) still picks up the global filter, because global filters apply to every request. The test that caused it passed. Run the same class with -Djunit.jupiter.execution.parallel.enabled=false, and all nine pass, because the filter is reset before the next test starts. That’s what makes these bugs hard: they only exist in parallel.
Remove the test and the annotation, and run the tests once more to confirm they pass.
Troubleshooting
| Symptom | Likely cause | Fix |
|---|---|---|
| A test fails only in parallel, with a reason not in its code | Another test set a static REST Assured default | Use specifications and per-request settings, never RestAssured.* statics |
The class still runs on main | No @Execution(CONCURRENT), or parallel execution isn’t enabled | Check the annotation and junit-platform.properties |
| Counts or collections are wrong at random | A non-thread-safe collection shared between tests | Use ConcurrentHashMap.newKeySet() and other concurrent collections |
| Two tests collide on the same record | Shared test data | Let each test create its own data |
| No speed-up | The API under test handles one request at a time | Check the API’s concurrency, or use fewer threads |
Try It Yourself
Exercise 1: Use Eight Threads
Run the class with eight threads instead of four, without changing any file, and compare the time and the number of threads with the four-thread run.
Hint
Configuration parameters can also be set as system properties on the command line, with -D. The one to change is junit.jupiter.execution.parallel.config.fixed.parallelism.
Solution
1
mvn test -pl module-12 -Dtest=ParallelExecutionTest -Djunit.jupiter.execution.parallel.config.fixed.parallelism=8
1
2
ParallelExecutionTest ran on 8 thread(s): [ForkJoinPool-1-worker-8, ForkJoinPool-1-worker-6, ...]
Tests run: 8, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.671 s
All eight requests ran in one wave. The gain over four threads is smaller than the gain over one, because the API’s start-up is a fixed part of the time.
Exercise 2: Hide the Bug Sequentially
Add the static-filter test from Make a Test Fail again, and run the class twice: once in parallel, and once with parallel execution turned off. Compare the results, and explain why a sequential build would never show this bug.
Hint
Turn parallel execution off with -Djunit.jupiter.execution.parallel.enabled=false. Think about when RestAssured.reset() runs relative to the other tests in each case.
Solution
1
2
mvn test -pl module-12 -Dtest=ParallelExecutionTest
mvn test -pl module-12 -Dtest=ParallelExecutionTest -Djunit.jupiter.execution.parallel.enabled=false
In parallel, eight of nine tests failed. Sequentially, all nine passed, in 3.2 seconds: the filter test ran first, its finally block reset the filter, and only then did the next test start. A suite that has always run sequentially can hide any number of these, which is why parallelism is introduced class by class.
Summary
You built ParallelExecutionTest, which runs eight independent reads on four threads in under a second, against over two seconds one after another, and you saw a single static filter fail every other test in the class, but only in parallel.
junit.jupiter.execution.parallel.enabled=trueturns parallel execution on; an opt-in set-up keeps everything sequential unless a class says@Execution(CONCURRENT).strategy=fixedandparallelismchoose the number of threads, and can be overridden with-Don the command line.- Build specifications once and share them; each
given(spec)is a new request. - Never set REST Assured’s static defaults in concurrent tests: even
given(spec)picks up a global filter. - Use concurrent collections for shared state in the test, and give each test its own data.
Here are the commands you used in this guide:
1
2
3
mvn test -pl module-12 -Dtest=ParallelExecutionTest
mvn test -pl module-12 -Dtest=ParallelExecutionTest -Djunit.jupiter.execution.parallel.enabled=false
mvn test -pl module-12 -Dtest=ParallelExecutionTest -Djunit.jupiter.execution.parallel.config.fixed.parallelism=8
What’s Next
Next: continue with Run the Right Subset of a REST Assured Suite With Tags and Maven Profiles, which runs the right subset of a large suite with JUnit tags and Maven profiles.
The code:
- The finished test class:
ParallelExecutionTest.java - The configuration:
junit-platform.properties - The code and run commands for this module: Module 12 README
Go deeper:
- JUnit User Guide: Parallel Execution, for every configuration parameter and execution mode.
Related guides:
- Inject REST Assured Specifications With a JUnit Extension, for giving tests specifications without static state.
- Test API Workflows With REST Assured, for independent tests that each create their own data.
