Spring: MockMvc tests a controller without a server

It runs the full Spring MVC stack in memory - mapping, binding, validation and your handler.

Code
@Test
void returnsOrder() throws Exception {
    given(service.byId(42L)).willReturn(new Order(42L, "JCH-01"));

    mvc.perform(get("/api/orders/42"))
       .andExpect(status().isOk())
       .andExpect(jsonPath("$.sku").value("JCH-01"));
}
Output
MockHttpServletResponse:
   Status = 200
     Body = {"id":42,"sku":"JCH-01"}
Advertisement

Run this yourself in the Online Java Compiler, spin up a live REST API in the API Sandbox, or practise with Java interview questions.

Published 2026-08-11