Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does MockMvc always return empty content()?

I'm trying to test my rest api with mockMvc.

mockMvc.perform(get("/users/1/mobile")
        .accept(MediaType.APPLICATION_JSON))
        .andExpect(status().isOk())
        .andDo(print())
        .andExpect(content().string("iPhone"))

The test failed because of:

java.lang.AssertionError: Response content 
Expected :iPhone
Actual   :

From the output of print(), I can know the API actually returned the expected string "iPhone".

ModelAndView:
        View name = users/1/mobile
             View = null
        Attribute = treeNode
            value = "iPhone"
           errors = []

And I guess the empty "Actual" above is caused by empty "Body" below

MockHttpServletResponse:
           Status = 200
    Error message = null
          Headers = {}
     Content type = null
             Body = 
    Forwarded URL = users/1/mobile
   Redirected URL = null
          Cookies = []

My questions are:

  1. Why MockHttpServletResponse's Body is empty;
  2. How can I correctly test the response of API.
like image 553
kuang Avatar asked Oct 16 '16 05:10

kuang


People also ask

How does MockMvc perform work?

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 MockMvc in spring?

What is MockMvc? MockMvc is a Spring Boot test tool class that lets you test controllers without needing to start an HTTP server. In these tests the application context is loaded and you can test the web layer as if i's receiving the requests from the HTTP server without the hustle of actually starting it.

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()); }

What is mockmvc in Spring MVC?

MockMvc is defined as a main entry point for server-side Spring MVC testing. Tests with MockMvc lie somewhere between between unit and integration tests. The following application uses MockMvc to test a Spring MVC application.

What is the difference between mockmvc and @webappconfiguration?

With MockMvc we are also able to test path, request method, headers, responses, etc. so we are getting the best of both worlds. The following test shows this behavior: The configuration differs a little from the previous. @WebAppConfiguration annotation is no longer used, so Spring won’t load a full application context (less overhead).

What is a template test with mockmvc?

Tests with MockMvc lie somewhere between between unit and integration tests. The following application uses MockMvc to test a Spring MVC application. We create a test for a template and for a RESTful controller method.

Can mockmvc be used to test a basic spring rest controller?

The post focuses only on testing a basic spring REST controller, although MockMvc can be used to test the complete suite of components from Spring MVC framework. The full source code for this post can be found at Github.


1 Answers

If your action methods (methods with @RequestMapping annotation) return instances of ModelAndView or you work with Model, you have to test it using MockMvcResultMatchers#model function:

.andExpect(MockMvcResultMatchers.model().attribute("phone", "iPhone"))
.andExpect(MockMvcResultMatchers.model().size(1))

MockMvcResultMatchers#content is appropriate for REST action methods (methods with @RequestBody annotation).


To have a better understanding about testing Spring MVC and Spring REST controllers check these links:

  • Testing of Spring MVC Applications: Forms
  • Testing of Spring MVC Applications: REST API
like image 151
Anton Dozortsev Avatar answered Sep 19 '22 08:09

Anton Dozortsev