@SpringBootTest webEnvironment Options
Fast recall
Quick revision
Core answer
Knowledge of @SpringBootTest configuration beyond just using the defaults, and which client tool pairs with which mode
Not knowing these options exist and then wondering why TestRestTemplate doesn't work with the default MOCK mode
Explain that RANDOM_PORT prevents port conflicts in CI and mention DEFINED_PORT as an option for debugging
Interview-ready guide
Interview answer
@SpringBootTest has four web-environment modes. MOCK is the default and creates a mock web context when supported, but it does not open a network port. RANDOM_PORT and DEFINED_PORT start an embedded server; the former asks the operating system for an available port, while the latter uses the configured port. NONE creates a non-web context. I use RANDOM_PORT when the real HTTP boundary matters and MOCK when no socket is needed. TestRestTemplate needs a running server, while WebTestClient can also work with supported direct bindings, so the client is not defined solely by the mode.
@SpringBootTest webEnvironment Options
Direct Answer
MOCK is the default: it creates a web ApplicationContext when the classpath supports one, without starting an embedded server, and servlet applications can add MockMvc. RANDOM_PORT starts a server on an available port; DEFINED_PORT starts it on the configured port; NONE creates a non-web context. The client can be TestRestTemplate, WebTestClient, or another suitable client.
Code Example
// MOCK (default): no real server, drive it with MockMvc
@SpringBootTest // webEnvironment = MOCK by default
@AutoConfigureMockMvc
class MockEnvTest {
@Autowired MockMvc mockMvc;
@Test
void hitsEndpoint() throws Exception {
mockMvc.perform(get("/api/orders"))
.andExpect(status().isOk());
}
}
// RANDOM_PORT: a real server starts, call it with TestRestTemplate
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
class RandomPortTest {
@LocalServerPort int port; // the random port, injected
@Autowired TestRestTemplate rest;
@Test
void callsRealHttp() {
var body = rest.getForObject(
"http://localhost:" + port + "/api/orders", String.class);
assertThat(body).isNotNull();
}
}
Swap MOCK for RANDOM_PORT the moment you need a real socket — TestRestTemplate has nothing to talk to under MOCK.
Common Mistakes & Pitfalls
- Using a network client with MOCK: MOCK does not open a listening server. Use MockMvc/WebTestClient against the mock context, or choose a port mode for real HTTP.
- Hard-coding the random port: An injected TestRestTemplate can use relative paths with its configured root URL. If another client builds an absolute URL, read the actual port with @LocalServerPort.
- Choosing DEFINED_PORT without isolation: A fixed port can collide when test processes run in parallel. Use it only when that exact port is part of the requirement and the environment reserves it.
- Omitting DEFINED_PORT from the model: It is a fourth option beside MOCK, RANDOM_PORT, and NONE.
Check your understanding
Answer these without looking back at the article.
- How do you get the actual port number when using RANDOM_PORT?
- What is @LocalServerPort?
Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.