Basic @WebMvcTest GET Request
Fast recall
Quick revision
Core answer
@WebMvcTest(MyController.class), inject MockMvc, provide any missing controller collaborator with the current @MockitoBean support (or @MockBean on older Spring Boot versions), stub it only when the response depends on it, then assert the real path: mockMvc.perform(get("/api/orders/1")).andExpect(status().isOk()).andExpect(jsonPath("$.id").value(1));.Hands-on ability to write a real controller test using MockMvc—this is the number one practical coding question in fresher interviews
Calling the wrong route, omitting a required collaborator, or asserting only 200 without checking the response contract
Walk through the test structure methodically: arrange (stub the mock), act (perform the request), assert (check status and body)
Interview-ready guide
Interview answer
I select the controller with @WebMvcTest, inject MockMvc, and provide any collaborator outside the slice with @MockitoBean, or @MockBean on older Boot. I arrange the exact result needed for the case, call the real public route with mockMvc.perform(get(...)), and assert status().isOk() plus the response contract—for example the content type and jsonPath("$.id"). Stubbing is scenario-driven: Mockito defaults do not always cause the same failure, so the test should state the collaborator behavior it needs.
Basic @WebMvcTest GET Request
Direct Answer
With @WebMvcTest(MyController.class), inject MockMvc, provide any missing controller collaborator with the current @MockitoBean support (or @MockBean on older Spring Boot versions), stub it only when the response depends on it, then assert the real path: mockMvc.perform(get("/api/orders/1")).andExpect(status().isOk()).andExpect(jsonPath("$.id").value(1));.
Code Example
@WebMvcTest(ProductController.class)
class ProductControllerTest {
@Autowired
private MockMvc mockMvc;
@MockBean
private ProductService productService;
@Test
void shouldReturnProductListWithOkStatus() throws Exception {
// Arrange: stub the service to return test data
var products = List.of(
new Product(1L, "Laptop", 1200.00),
new Product(2L, "Mouse", 25.00)
);
when(productService.getAllProducts()).thenReturn(products);
// Act & Assert: perform GET and verify response
mockMvc.perform(get("/api/products"))
.andExpect(status().isOk()) // Verify HTTP 200
.andExpect(jsonPath("$[0].name").value("Laptop"))
.andExpect(jsonPath("$[1].price").value(25.00));
}
}
Common Mistakes & Pitfalls
- Using a different route from the controller mapping: Assert the actual public path, including path variables and query parameters.
- Forgetting a needed stub: Mockito returns type-dependent defaults; the resulting response depends on the controller, so arrange the specific result the scenario requires.
- Asserting only the status: Also verify stable contract fields, content type, headers, or error body that matter to the client.
- Wrong JSONPath:
$represents the JSON document root; arrays use paths such as$[0].name.
Check your understanding
Answer these without looking back at the article.
- How do you test a POST endpoint with a request body in MockMvc?
- What is jsonPath() and how does it work in MockMvc assertions?
Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.