REST Web Services testing with Spring MVC


A previous post introduced the basic features of the Spring MVC test framework. This post covers the testing of a REST web controller: by checking the response status, the content type, and the returned JSON document , we’ll verify if the controller behaves as expected.

The controller to test

The controller to test is rather simple, it just returns a Java object that ends up being serialized in the response. The point here is to test Spring MVC features like argument mapping and serialization.

The test of the controller

Setting up the test

We need to set up the web version (@WebAppConfiguration) of the Spring TestContext Framework (@RunWith(SpringJUnit4ClassRunner.class) and @ContextConfiguration). We embed the configuration in the test, thanks to a @Configuration inner class. Here is the setup part of the test:

Note we use @EnableWebMvc on the configuration class: this will activate (among others) the REST support. We need this to register the Jackson (JSON) converter in charge of serialization.

The infrastructure is ready, let’s send a (mock) request to our controller.

Sending a request, debugging the response

The whole point of the test is to ensure the controller responds correctly to one of our requests. Our controller is quite simple, so we could send a request and directly check the result with the appropriate expectation API. For more complex cases, like fancy custom JSON serialization, we would typically send the request and use some debugging features to display the response. This would make the writing of the expectations simpler (especially if the returned document is somewhat complex). Let’s do that!

Our first version of the test method sends the request and use andDo(print()) to display the request and response information on the console:

The execution of the test should output the following on the console:

In real-world tests, you would typically check the response status, the headers, and the body:

Let’s see how to do that.

Checking the response

We check the response by calling the andExpect method on the ResultActions object returned by the perform method. The andExpect needs a ResultMatcher object and we would typically use ResultMatchers provided by the MockMvcResultMatchers class.

To check the response code, we use the status() method. We expect 200 (OK):

To check the content type:

Testing the JSON document can be a pain, especially if you don’t have support for JSON parsing. The good news is Spring MVC test framework supports JsonPath out-of-the-box. Retrieving the value of the id attribute is like the following:

Let’s put it all together, here is the full test method:

Pretty simple, isn’t it? Launch the test in your favorite IDE to see the green bar!

And here’s the full version of the test:


This post covered how to test a Spring MVC REST controller with the Spring MVC out-of-container test framework. We managed to check the controller in terms of input parameters, response status, response headers, and response content. By testing a read-only web service, we only scratched the surface of the framework: one can test the other HTTP operations (POST, PUT, DELETE), requests with some content in the body, more headers, etc. Now, get back to work and test your REST web service layer!

Source code

Partagez cet article.

A propos de l'auteur

Un commentaire

  1. Hi
    Great post and everything clearly explained.
    I was wondering how to compare string from response body (not json) with that andExpect()
    I know that mockMvc has andReturn() so whole expression can return response – but I wonder that is possible to check simple string against that in body within mockMvc expression?

Ajouter un commentaire