单元测试Spring MissingServletRequestParameterException JSON响应

问题描述:

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

I have POST method in a Spring boot rest controller as follows

@RequestMapping(value="/post/action/bookmark", method=RequestMethod.POST)
public @ResponseBody Map<String, String> 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正文:

now if I test with missing parameter in Postman I get an 400 http response and a JSON body:

{
  "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"
}

直到现在没关系,但是当我尝试进行单元测试时,我没有得到JSON回复

until now it's OK, but when I try to unit test I don't get the JSON response back

@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()),发现回复中没有正文

I did a .andDo(print()) and found that there is no body in the response

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 = []

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

why am I not getting the JSON response while unit testing my controller, but do receive it in manual testing using Postman or cUrl?

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

I've added @WebIntegrationTest but got the same error:

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已经自动配置了一个异常处理程序 org.springframework.boot.autoconfigure.web.BasicErrorController 这可能不存在在你的单元测试中。获得它的方法是使用Spring Boot测试支持相关的注释:

This is because Spring Boot has auto-configured an exception handler org.springframework.boot.autoconfigure.web.BasicErrorController which is probably not present in your unit tests. A way to get it will be to use the Spring Boot testing support related annotations:

@SpringApplicationConfiguration
@WebIntegrationTest

更多细节是这里

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

Update: You are absolutely right, the behavior is very different in UI vs in test, the error pages which respond to status codes are not correctly hooked up in a non-servlet test environment. Improving this behavior can be a good bug to open for Spring MVC and/or Spring Boot.

目前,我有一个模拟 BasicErrorController 以下方式:

For now, I have a workaround which simulates the behavior of BasicErrorController the following way:

@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<Map<String, Object>> error(HttpServletRequest request) {
        return super.error(request);
    }
}

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

What I am doing here is adding a ControllerAdvice which handles the Exception flow and delegates back to the BasicErrorController. This would atleast make the behavior consistent for you.