Testing POST Endpoint with MockMvc
Fast recall
Quick revision
Core answer
contentType(MediaType.APPLICATION_JSON) and a JSON body, then assert the status, headers, and response fields defined by that endpoint. ObjectMapper.writeValueAsString(request) keeps the body consistent with Java serialization. A creation endpoint commonly returns 201, but the API contract decides the expected status.Practical MockMvc knowledge for POST requests—slightly harder than GET and very commonly asked in interviews
Forgetting to set contentType(APPLICATION_JSON), which causes HTTP 415 Unsupported Media Type, or not knowing how to serialize the request body
Show both manual JSON string construction and using ObjectMapper to serialize a Java object, and explain why content type is required
Interview-ready guide
Interview answer
For a POST endpoint that consumes JSON, I select the controller with @WebMvcTest, inject MockMvc and ObjectMapper, and provide any missing collaborator. I serialize a request object with writeValueAsString, set application/json, and perform the real route. Then I assert the response defined by that endpoint—for a normal creation this may be 201, a Location header, and the generated id. MVC deserializes the body into a new object, so Mockito setup should match meaningful arguments rather than the original object's identity. I also cover invalid JSON and validation errors. This proves the MVC contract; persistence and transaction behavior need the appropriate broader test.
Testing POST Endpoint with MockMvc
Direct Answer
For an endpoint that consumes JSON, perform the real POST route with contentType(MediaType.APPLICATION_JSON) and a JSON body, then assert the status, headers, and response fields defined by that endpoint. ObjectMapper.writeValueAsString(request) keeps the body consistent with Java serialization. A creation endpoint commonly returns 201, but the API contract decides the expected status.
Code Example
@WebMvcTest(OrderController.class)
class OrderControllerTest {
@Autowired
private MockMvc mockMvc;
@Autowired
private ObjectMapper objectMapper; // Injected by Spring
@MockBean
private OrderService orderService;
@Test
void shouldCreateOrderAndReturn201() throws Exception {
var newOrder = new Order(null, "Gaming PC", 1500.00);
var savedOrder = new Order(42L, "Gaming PC", 1500.00);
when(orderService.createOrder(any(Order.class))).thenReturn(savedOrder);
mockMvc.perform(post("/api/orders")
.contentType(MediaType.APPLICATION_JSON) // Required for JSON body
.content(objectMapper.writeValueAsString(newOrder))) // Serialize to JSON
.andExpect(status().isCreated())
.andExpect(jsonPath("$.id").value(42))
.andExpect(jsonPath("$.item").value("Gaming PC"));
}
}
Common Mistakes & Pitfalls
- Using the wrong media type: A JSON-consuming mapping may return 415 when the request omits or changes
application/json; assert the behavior defined by that mapping. - Writing fragile JSON by hand: Use ObjectMapper for normal request objects, and write raw JSON only when malformed input is the test case.
- Matching the exact pre-serialization object: MVC creates a different request instance. Match meaningful arguments or capture and inspect them rather than depending on object identity.
- Assuming every POST returns 201: Creation commonly uses 201, while commands, searches, and asynchronous work can use other contracts. Assert the endpoint specification.
Check your understanding
Answer these without looking back at the article.
- How do you test validation errors — for example, when a required field is missing?
- How do you assert the response body's JSON structure with jsonPath?
Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.