Java 单元测试Spring缺少ServletRequestParameterException JSON响应
我在Spring boot rest控制器中有如下POST方法Java 单元测试Spring缺少ServletRequestParameterException JSON响应,java,spring-boot,spring-test-mvc,spring-restcontroller,jsonresponse,Java,Spring Boot,Spring Test Mvc,Spring Restcontroller,Jsonresponse,我在Spring boot rest控制器中有如下POST方法 @RequestMapping(value="/post/action/bookmark", method=RequestMethod.POST) public @ResponseBody Map<String, String> bookmarkPost( @RequestParam(value="actionType",required=true) String actionType,
@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);
}
直到现在还可以,但是当我尝试进行单元测试时,我没有得到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 = []
为什么我在单元测试控制器时没有得到JSON响应,但在使用Postman或cUrl进行手动测试时收到了JSON响应
编辑:我添加了@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
}
}
这是因为SpringBoot已经自动配置了一个异常处理程序
org.springframework.Boot.autoconfigure.web.BasicErrorController
,它可能在单元测试中不存在。一种方法是使用Spring Boot测试支持相关注释:
@SpringApplicationConfiguration
@WebIntegrationTest
更多详情请参阅
更新:
您完全正确,UI和测试中的行为非常不同,响应状态代码的错误页面在非servlet测试环境中没有正确连接。对于SpringMVC和/或SpringBoot来说,改进这种行为可能是一个很好的bug
目前,我有一个变通方法,可以通过以下方式模拟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<Map<String, Object>> error(HttpServletRequest request) {
return super.error(request);
}
}
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(类={RestApplication.class,TestConfiguration.class})
@网络集成测试
公共类后控制器测试{
私有MockMvc-MockMvc;
@自动连线
私有WebApplicationContext WebApplicationContext;
@自动连线
私有过滤链Proxy springSecurityFilterChain;
@以前
公共作废设置(){
mockMvc=MockMvcBuilders.webAppContextSetup(webApplicationContext)
.addFilter(springSecurityFilterChain)
.build();
}
@试验
public void bookmarkMissingActionTypeParam()引发异常{
//@formatter:off
mockMvc.perform(
帖子(“/post/action/bookmark”)
.accept(MediaType.APPLICATION_JSON)
.param(“postId”,“55ab8831036437e96e8250b6”)
)
.andDo(print())
.andExpect(状态().isBadRequest())
.andExpect(jsonPath($.exception),包含字符串(“MissingServletRequestParameterException”);
//@formatter:on
}
}
@配置
公共静态类TestConfiguration{
@豆子
公共ErrorController ErrorController(ErrorAttributes ErrorAttributes){
返回新的ErrorController(errorAttributes);
}
}
@控制器建议
类ErrorController扩展了BasicErrorController{
公共ErrorController(ErrorAttributes ErrorAttributes){
超级(错误属性);
}
@凌驾
@ExceptionHandler(Exception.class)
@应答器
公共响应性错误(HttpServletRequest){
返回超级错误(请求);
}
}
我在这里所做的是添加一个
ControllerAdvice
,它处理异常流并委托回BasicErrorController
。这至少会使你的行为一致 最初,在定义REST控制器方法时,它应该通过@ResponseBody
标记来修复错误。它将修复测试类中的json错误。
但是,当您使用spring boot时,您将使用
@RestController
定义控制器类,并且它应该自动处理错误,而无需定义@controller
和@ResponseType
标记 我添加了@WebIntegrationTest,但得到了相同的错误,我在上面添加了完整的测试类只是为了提供信息,您的重新启动应用程序是否具有@EnableAutoConfiguration
注释。否,仅使用'@springbootplication'。。这不是等价的吗?你是对的,我添加了一个更新,有一个潜在的解决方法。我面临着同样的问题,但仍然没有解决。我也遇到了同样的问题,没有错误的响应:你成功了吗?不,还没有。。我跳过了测试这些案例!我也面临着同样的问题,这个答案没有帮助,@Biju以正确的方式理解他们的问题。
@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);
}
}