Scrivio Interview library
ConceptFoundations5 min read · updated 12 November 2026

Explain the webEnvironment options of @SpringBootTest (MOCK, RANDOM_PORT, DEFINED_PORT, NONE) and when you would use each when testing a Spring MVC service.

MOCK, the default, builds a web application context with a mock servlet environment and no real server, which is what MockMvc needs. RANDOM_PORT starts the embedded server on a free port for real HTTP tests with TestRestTemplate, WebTestClient or RestTestClient, and we read the port with @LocalServerPort. DEFINED_PORT starts the server on the configured port and is rarely worth the risk of port clashes, and NONE builds a plain, non-web context for testing services, jobs and listeners.

Why this question is asked

@SpringBootTest starts our whole application inside a test, and the webEnvironment attribute decides how much of the web side comes with it. The choice changes what we can test, how fast the test is, and whether two builds on the same CI machine can collide. Interviewers ask it because it shows whether we know the difference between testing through a fake request and testing through a real socket. In this answer, we will recap what @SpringBootTest does, go through the four options with a small test for each, and finish with how to choose.

1A Quick Recap of @SpringBootTest

@SpringBootTest tells the Spring TestContext framework, the part of Spring that runs tests with a Spring context, to start our application the way SpringApplication would: it finds our @SpringBootApplication class, applies auto-configuration and creates every bean. The test class then gets beans injected with @Autowired.

For a Spring MVC service, a web context normally includes an embedded servlet container, a web server such as Tomcat that runs inside the JVM and listens on a port. webEnvironment controls whether that server really starts. The four values are MOCK, RANDOM_PORT, DEFINED_PORT and NONE.

2MOCK: A Web Context Without a Server

MOCK is the default. Spring creates a web application context, so controllers, filters and MVC configuration all exist, but the servlet environment is a mock: there is no Tomcat and no port. We send requests through MockMvc, which calls the DispatcherServlet directly with fake request and response objects. With @SpringBootTest we add @AutoConfigureMockMvc to get a MockMvc bean.

Java
@SpringBootTest // webEnvironment = MOCK by default
@AutoConfigureMockMvc
class OrderApiMockTest {
@Autowired MockMvc mvc;
@Test
void findsOrder() throws Exception {
mvc.perform(get("/orders/42"))
.andExpect(status().isOk()); // 200, no socket opened
}
}

Notice that this is fast and runs the full application behind the controller. The cost is that container behaviour, such as error page forwarding and connector settings, is not exercised. If the servlet API is not on the classpath at all, MOCK quietly falls back to a non-web context.

3RANDOM_PORT: A Real Server on a Free Port

RANDOM_PORT starts the embedded server for real, on a port the operating system picks as free. Requests now travel over HTTP through the real container. We inject the chosen port with @LocalServerPort, or let Spring Boot hand us a client already pointed at it.

Java
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
class OrderApiHttpTest {
@LocalServerPort int port; // for example 52314
@Autowired WebTestClient client; // already bound to http://localhost:52314
@Test
void findsOrder() {
client.get().uri("/orders/42").exchange()
.expectStatus().isOk(); // 200 through real Tomcat
}
}

This is the option for end-to-end HTTP tests: real serialization over the wire, real error pages, real filters registered with the container. The client can be TestRestTemplate, WebTestClient, or on Spring Framework 7 and Spring Boot 4, RestTestClient. Which ones Boot registers automatically, and which extra annotation or dependency each needs, changed in Boot 4, so check your version. One thing to remember: the test and the server run on different threads, so a @Transactional test does not roll back what the server wrote.

4DEFINED_PORT: The Configured Port

DEFINED_PORT also starts a real server, but on the port from server.port, 8080 if nothing is set. It exists for the rare case where something outside the test needs a fixed address, such as a browser driver configured with a fixed URL.

The risk is a clash: if two builds run on the same CI agent, or a developer has the application running locally, the second server fails with a port already in use error. That is why we prefer RANDOM_PORT and pass the port along wherever it is needed.

5NONE: No Web Context at All

NONE creates an ordinary application context with no web environment, as if spring.main.web-application-type were none. Controllers are still beans if they are scanned, but there is no DispatcherServlet and no server. It suits tests of services, scheduled jobs, batch steps or message listeners, where the web layer would only slow startup.

Java
@SpringBootTest(webEnvironment = WebEnvironment.NONE)
class InvoiceJobTest {
@Autowired InvoiceJob job;
@Test
void createsInvoices() {
assertThat(job.runFor(LocalDate.of(2026, 1, 31))).isEqualTo(17); // 17 invoices
}
}
Choosing a webEnvironment
OptionServer started?Typical clientUse it for
MOCK (default)No, mock servlet environmentMockMvcMost controller and full-stack tests
RANDOM_PORTYes, on a free portWebTestClient, RestTestClientReal HTTP and container behaviour
DEFINED_PORTYes, on server.portAny HTTP clientRare fixed-address needs
NONENo web contextBeans directlyServices, jobs, listeners

6Follow-up questions

Why does a @Transactional test not roll back data in a RANDOM_PORT test?

The transaction belongs to the test thread, but the request is handled by a server thread with its own transaction, which commits normally. So data written through HTTP stays in the database. We clean up explicitly, for example by truncating tables before each test, or use separate data per test.

Do MOCK and RANDOM_PORT tests share an application context?

No. The web environment is part of the context's cache key, so a MOCK test class and a RANDOM_PORT test class each start their own context. Keeping most tests on one option helps the cache.

Can I inject the port in a non-test bean?

The port is available as the local.server.port property once the server has started, which is what @LocalServerPort reads. It is not known when beans are created, so beans that need it should read it later, or the test should pass it in.

7Conclusion

In this answer, we saw that MOCK, the default, gives a web context with a mock servlet environment for MockMvc and no server; RANDOM_PORT starts a real server on a free port for HTTP tests, with the port injected by @LocalServerPort; DEFINED_PORT uses server.port and risks clashes, so we rarely use it; and NONE builds a non-web context for testing services, jobs and listeners.

What to say in the interviewAbout 70 seconds aloud

Open with

webEnvironment decides whether @SpringBootTest starts a real server. MOCK is the default: it creates a web application context with a mock servlet environment, so there is no Tomcat and no port, and I test controllers through MockMvc, adding @AutoConfigureMockMvc.

Then, if they want more

That is my default for most web tests because it is fast and still runs the whole application. RANDOM_PORT starts the embedded server on a free port, and I use it when I want a real HTTP round trip, with WebTestClient, TestRestTemplate or, on Spring Boot 4, RestTestClient. I inject the port with @LocalServerPort if I need it.

I keep those tests few, for things MockMvc does not cover, like error pages or connector settings, and I remember that a test transaction does not roll back what the server thread committed. DEFINED_PORT also starts a real server, but on server.port, 8080 by default, so two builds on one machine can clash; I almost never use it.

NONE builds a context without any web environment, which I use for testing services, scheduled jobs or message listeners where the web layer would only slow things down.

About this answer

Last updated
12 November 2026
Versions it names
Spring Boot 4, Spring Framework 7
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
Spring Boot reference, Testing
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 "Explain the webEnvironment options of @SpringBootTest (MOCK, RANDOM_PORT, DEFINED_PORT...": https://scrivio-interview-library.netlify.app/spring-boot/spring-boot-test/f6c33bcf5f/