java testresttemplate example - Difference between MockMvc and RestTemplate in integration tests
MockMvc, you're typically setting up a whole web application context and mocking the HTTP requests and responses. So, although a fake
DispatcherServlet is up and running, simulating how your MVC stack will function, there are no real network connections made.
RestTemplate, you have to deploy an actual server instance to listen for the HTTP requests you send.
Question is: what's the difference between them and when we should choose one over another?
Here are just examples of both options:
//MockMVC example mockMvc.perform(get("/api/users")) .andExpect(status().isOk()) (...) //RestTemplate example ResponseEntity<User> entity = restTemplate.exchange("/api/users", HttpMethod.GET, new HttpEntity<String>(...), User.class); assertEquals(HttpStatus.OK, entity.getStatusCode());