java testresttemplate - Difference between MockMvc and RestTemplate in integration tests

example mockrestserviceserver (4)

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:

public class SampleTests {

  private WebApplicationContext wac;

  private MockMvc mockMvc;

  public void setup() {
    this.mockMvc = webAppContextSetup(this.wac).build();

  public void getFoo() throws Exception {

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.


RestTemplate restTemplate = new RestTemplate();
MockRestServiceServer mockServer = MockRestServiceServer.createServer(restTemplate);

  .andRespond(withSuccess("Hello world", "text/plain"));

// use RestTemplate ...


also read this example

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

//RestTemplate example
ResponseEntity<User> entity ="/api/users",
            new HttpEntity<String>(...),
assertEquals(HttpStatus.OK, entity.getStatusCode());

It is possible to use both RestTemplate and MockMvc!

This is useful if you have a separate client where you already do the tedious mapping of Java objects to URLs and converting to and from Json, and you want to reuse that for your MockMVC tests.

Here is how to do it:

public class MyTestShould {

    private MockMvc mockMvc;

    public void verify_some_condition() throws Exception {

        MockMvcClientHttpRequestFactory requestFactory = new MockMvcClientHttpRequestFactory(mockMvc);
        RestTemplate restTemplate = new RestTemplate(requestFactory);

        ResponseEntity<SomeClass> result = restTemplate.getForEntity("/my/url", SomeClass.class);



With 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.

With RestTemplate, you have to deploy an actual server instance to listen for the HTTP requests you send.

There are several differences between HashMap and Hashtable in Java:

  1. Hashtable is synchronized, whereas HashMap is not. This makes HashMap better for non-threaded applications, as unsynchronized Objects typically perform better than synchronized ones.

  2. Hashtable does not allow null keys or values. HashMap allows one null key and any number of null values.

  3. One of HashMap's subclasses is LinkedHashMap, so in the event that you'd want predictable iteration order (which is insertion order by default), you could easily swap out the HashMap for a LinkedHashMap. This wouldn't be as easy if you were using Hashtable.

Since synchronization is not an issue for you, I'd recommend HashMap. If synchronization becomes an issue, you may also look at ConcurrentHashMap.

java spring spring-mvc junit integration-testing