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:
MockHttpServletResponse's
Body is empty;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? 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.
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()); }
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.
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).
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.
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.
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:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With