Q18 of 40 · REST Assured
How would you integrate REST Assured tests with TestNG vs JUnit 5? What changes?
Short answer
Short answer: With JUnit 5, use @BeforeAll/@Test from org.junit.jupiter.api — no REST Assured config needed. TestNG uses @BeforeClass/@Test from org.testng. The main difference is parallel execution config: JUnit 5 uses junit-platform.properties; TestNG uses the XML suite file with parallel and thread-count attributes.
Detail
REST Assured is framework-agnostic — it is just a library, so the only difference is the test lifecycle annotations and how you configure parallelism.
JUnit 5 setup:
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
class UserApiTest extends BaseApiTest {
@BeforeAll static void init() { /* build specs */ }
@Test void getUser_returnsUser() { ... }
}
Parallel execution via junit-platform.properties:
junit.jupiter.execution.parallel.enabled = true
junit.jupiter.execution.parallel.mode.default = concurrent
junit.jupiter.execution.parallel.config.strategy = fixed
junit.jupiter.execution.parallel.config.fixed.parallelism = 4
TestNG setup:
public class UserApiTest extends BaseApiTest {
@BeforeClass public void init() { /* build specs */ }
@Test public void getUser_returnsUser() { ... }
}
Parallel execution in testng.xml:
<suite name="API Suite" parallel="methods" thread-count="4">
Key risk for both: REST Assured is thread-safe when you use local RequestSpecification instances. Static RestAssured.* fields (baseURI, port) are shared — use given(localSpec) rather than the static defaults when running parallel tests.
// EXAMPLE
BaseApiTest.java
// JUnit 5 version
@ExtendWith(SoftAssertionsExtension.class)
public abstract class BaseApiTestJUnit5 {
protected static RequestSpecification reqSpec;
protected static ResponseSpecification resSpec;
@BeforeAll
static void buildSpecs() {
reqSpec = new RequestSpecBuilder()
.setBaseUri(System.getenv("API_BASE_URL"))
.addHeader("Authorization", "Bearer " + getToken())
.setContentType(ContentType.JSON)
.build();
resSpec = new ResponseSpecBuilder()
.expectContentType(ContentType.JSON)
.build();
}
}
// TestNG version — same content, different annotations
public abstract class BaseApiTestTestNG {
protected static RequestSpecification reqSpec;
@BeforeClass
public void buildSpecs() { // note: not static in TestNG
reqSpec = new RequestSpecBuilder()
.setBaseUri(System.getenv("API_BASE_URL"))
.addHeader("Authorization", "Bearer " + getToken())
.build();
}
}