unit testingSpring MissingServletRequestParameterException JSON响应

我在Spring引导rest控制器中有POST方法,如下所示

@RequestMapping(value="/post/action/bookmark", method=RequestMethod.POST) public @ResponseBody Map bookmarkPost( @RequestParam(value="actionType",required=true) String actionType, @RequestParam(value="postId",required=true) String postId, @CurrentUser User user) throws Exception{ return service.bookmarkPost(postId, actionType, user); } 

现在,如果我在Postman中测试缺少参数,我会获得400个http响应和一个JSON正文:

 { "timestamp": "2015-07-20", "status": 400, "error": "Bad Request", "exception": "org.springframework.web.bind.MissingServletRequestParameterException", "message": "Required String parameter 'actionType' is not present", "path": "/post/action/bookmark" } 

直到现在它没关系,但是当我尝试进行unit testing时,我没有得到JSON响应

 @Test public void bookmarkMissingActionTypeParam() throws Exception{ // @formatter:off mockMvc.perform( post("/post/action/bookmark") .accept(MediaType.APPLICATION_JSON) .param("postId", "55ab8831036437e96e8250b6") ) .andExpect(status().isBadRequest()) .andExpect(jsonPath("$.exception", containsString("MissingServletRequestParameterException"))); // @formatter:on } 

测试失败并产生

 java.lang.IllegalArgumentException: json can not be null or empty 

我做了一个.andDo(print()) ,发现响应中没有正文

 MockHttpServletResponse: Status = 400 Error message = Required String parameter 'actionType' is not present Headers = {X-Content-Type-Options=[nosniff], X-XSS-Protection=[1; mode=block], Cache-Control=[no-cache, no-store], Pragma=[no-cache], Expires=[1], X-Frame-Options=[DENY]} Content type = null Body = Forwarded URL = null Redirected URL = null Cookies = [] 

为什么我在unit testing我的控制器时没有得到JSON响应,但是在使用Postman或cUrl进行手动测试时是否收到它?

编辑:我添加了@WebIntegrationTest但得到了同样的错误:

 import static org.hamcrest.Matchers.containsString; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.SpringApplicationConfiguration; import org.springframework.boot.test.WebIntegrationTest; import org.springframework.http.MediaType; import org.springframework.security.web.FilterChainProxy; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import org.springframework.test.web.servlet.MockMvc; import org.springframework.test.web.servlet.setup.MockMvcBuilders; import org.springframework.web.context.WebApplicationContext; @RunWith(SpringJUnit4ClassRunner.class) @SpringApplicationConfiguration(classes = RestApplication.class) @WebIntegrationTest public class PostControllerTest { private MockMvc mockMvc; @Autowired private WebApplicationContext webApplicationContext; @Autowired private FilterChainProxy springSecurityFilterChain; @Before public void setUp() { mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext) .addFilter(springSecurityFilterChain) .build(); } @Test public void bookmarkMissingActionTypeParam() throws Exception{ // @formatter:off mockMvc.perform( post("/post/action/bookmark") .accept(MediaType.APPLICATION_JSON) .param("postId", "55ab8831036437e96e8250b6") ) .andDo(print()) .andExpect(status().isBadRequest()) .andExpect(jsonPath("$.exception", containsString("MissingServletRequestParameterException"))); // @formatter:on } } 

这是因为Spring Boot已经自动配置了一个exception处理程序org.springframework.boot.autoconfigure.web.BasicErrorController ,它可能不存在于您的unit testing中。 获得它的方法是使用Spring Boot测试支持相关的注释:

 @SpringApplicationConfiguration @WebIntegrationTest 

更多细节在这里

更新 :你是绝对正确的,UI与测试中的行为非常不同,响应状态代码的错误页面未正确连接到非servlet测试环境中。 改进此行为可能是为Spring MVC和/或Spring Boot打开的一个很好的错误。

现在,我有一个解决方法,通过以下方式模拟BasicErrorController的行为:

 @RunWith(SpringJUnit4ClassRunner.class) @SpringApplicationConfiguration(classes = {RestApplication.class, TestConfiguration.class}) @WebIntegrationTest public class PostControllerTest { private MockMvc mockMvc; @Autowired private WebApplicationContext webApplicationContext; @Autowired private FilterChainProxy springSecurityFilterChain; @Before public void setUp() { mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext) .addFilter(springSecurityFilterChain) .build(); } @Test public void bookmarkMissingActionTypeParam() throws Exception{ // @formatter:off mockMvc.perform( post("/post/action/bookmark") .accept(MediaType.APPLICATION_JSON) .param("postId", "55ab8831036437e96e8250b6") ) .andDo(print()) .andExpect(status().isBadRequest()) .andExpect(jsonPath("$.exception", containsString("MissingServletRequestParameterException"))); // @formatter:on } } @Configuration public static class TestConfiguration { @Bean public ErrorController errorController(ErrorAttributes errorAttributes) { return new ErrorController(errorAttributes); } } @ControllerAdvice class ErrorController extends BasicErrorController { public ErrorController(ErrorAttributes errorAttributes) { super(errorAttributes); } @Override @ExceptionHandler(Exception.class) @ResponseBody public ResponseEntity> error(HttpServletRequest request) { return super.error(request); } } 

我在这里做的是添加一个ControllerAdvice来处理Exception流并委托回BasicErrorController 。 这至少会使行为与您保持一致。

最初,它应该在定义REST控制器方法时通过@ResponseBody标记修复错误。 它将修复测试类中的json错误。 但是,当您使用spring boot时,您将使用@RestController定义控制器类,它应该自动处理错误而不定义@Controller@ResponseType标记。