Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between MockMvc and RestTemplate in integration tests

Both MockMvc and RestTemplate are used for integration tests with Spring and JUnit.

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()); 
like image 334
Denis C de Azevedo Avatar asked Sep 17 '14 23:09

Denis C de Azevedo


People also ask

What is MockMvc test?

MockMVC class is part of Spring MVC test framework which helps in testing the controllers explicitly starting a Servlet container. In this MockMVC tutorial, we will use it along with Spring boot's WebMvcTest class to execute Junit testcases which tests REST controller methods written for Spring boot 2 hateoas example.

What is the use of MockMvc?

MockMvc provides support for Spring MVC testing. It encapsulates all web application beans and makes them available for testing. We'll initialize the mockMvc object in the @BeforeEach annotated method, so that we don't have to initialize it inside every test.

Can we use Mockito for integration test?

Even if Mockito (see my previous post) usually helps us to cut all dependencies in our unit tests, it can be useful also in the integration tests where we often want to have all real applications layers.

What is MockMvc standaloneSetup?

standaloneSetup() allows to register one or more controllers without the need to use the full WebApplicationContext . @Test public void testHomePage() throws Exception { this.mockMvc.perform(get("/")) .andExpect(status().isOk()) .andExpect(view().name("index")) .andDo(MockMvcResultHandlers.print()); }


1 Answers

As said in this article you should use MockMvc when you want to test Server-side of application:

Spring MVC Test builds on the mock request and response from spring-test and does not require a running servlet container. The main difference is that actual Spring MVC configuration is loaded through the TestContext framework and that the request is performed by actually invoking the DispatcherServlet and all the same Spring MVC infrastructure that is used at runtime.

for example:

@RunWith(SpringJUnit4ClassRunner.class) @WebAppConfiguration @ContextConfiguration("servlet-context.xml") public class SampleTests {    @Autowired   private WebApplicationContext wac;    private MockMvc mockMvc;    @Before   public void setup() {     this.mockMvc = webAppContextSetup(this.wac).build();   }    @Test   public void getFoo() throws Exception {     this.mockMvc.perform(get("/foo").accept("application/json"))         .andExpect(status().isOk())         .andExpect(content().mimeType("application/json"))         .andExpect(jsonPath("$.name").value("Lee"));   }} 

And RestTemplate you should use when you want to test Rest Client-side application:

If you have code using the RestTemplate, you’ll probably want to test it and to that you can target a running server or mock the RestTemplate. The client-side REST test support offers a third alternative, which is to use the actual RestTemplate but configure it with a custom ClientHttpRequestFactory that checks expectations against actual requests and returns stub responses.

example:

RestTemplate restTemplate = new RestTemplate(); MockRestServiceServer mockServer = MockRestServiceServer.createServer(restTemplate);  mockServer.expect(requestTo("/greeting"))   .andRespond(withSuccess("Hello world", "text/plain"));  // use RestTemplate ...  mockServer.verify(); 

also read this example

like image 149
nivash Avatar answered Sep 29 '22 14:09

nivash