将空数组参数传递给 MockHttpServletRequestBuilder 参数方法
Passing Empty array parameter to MockHttpServletRequestBuilder param method
我正在使用 MockHttpServletRequestBuilder 创建我的框架测试请求。我拥有的请求参数之一是字符串数组。对于我的测试,我想通过在我的请求中传递一个空数组来进行测试。但是,MockHttpServletRequestBuilder.addToMultiValueMap 中的断言不允许我传递 null 或空值。
这是我收到的错误正文:
java.lang.IllegalArgumentException: 'values' must not be empty
at org.springframework.util.Assert.notEmpty(Assert.java:214)
at org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder.addToMultiValueMap(MockHttpServletRequestBuilder.java:698)
at org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder.param(MockHttpServletRequestBuilder.java:153)
at MyMockApiRequests.queryPath(MockJdbcApiRequests.java:105)
有没有其他方法可以测试这种情况?
我认为将您的值设置为空字符串是可行的。
我的控制器
@RequestMapping(value = "/admin/config", method = RequestMethod.GET)
public List<TdConfig> getTdConfigs(@RequestParam(required = false) List<String> names) {
return tdAdminService.getTdConfigs(names);
}
我的 Mockito
MultiValueMap<String, String> params = new LinkedMultiValueMap<>();
params.add("names", "");
MockHttpServletRequestBuilder request = get("/admin/config")
.params(params)
.accept(MediaType.APPLICATION_JSON);
mockMvc.perform(request)
.andExpect(MockMvcResultMatchers.status().isOk())
.andDo(print());
请求的打印输出:
MockHttpServletRequest:
HTTP Method = GET
Request URI = /admin/config
Parameters = {names=[]}
Headers = {Accept=[application/json]}
我正在使用 MockHttpServletRequestBuilder 创建我的框架测试请求。我拥有的请求参数之一是字符串数组。对于我的测试,我想通过在我的请求中传递一个空数组来进行测试。但是,MockHttpServletRequestBuilder.addToMultiValueMap 中的断言不允许我传递 null 或空值。
这是我收到的错误正文:
java.lang.IllegalArgumentException: 'values' must not be empty
at org.springframework.util.Assert.notEmpty(Assert.java:214)
at org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder.addToMultiValueMap(MockHttpServletRequestBuilder.java:698)
at org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder.param(MockHttpServletRequestBuilder.java:153)
at MyMockApiRequests.queryPath(MockJdbcApiRequests.java:105)
有没有其他方法可以测试这种情况?
我认为将您的值设置为空字符串是可行的。
我的控制器
@RequestMapping(value = "/admin/config", method = RequestMethod.GET)
public List<TdConfig> getTdConfigs(@RequestParam(required = false) List<String> names) {
return tdAdminService.getTdConfigs(names);
}
我的 Mockito
MultiValueMap<String, String> params = new LinkedMultiValueMap<>();
params.add("names", "");
MockHttpServletRequestBuilder request = get("/admin/config")
.params(params)
.accept(MediaType.APPLICATION_JSON);
mockMvc.perform(request)
.andExpect(MockMvcResultMatchers.status().isOk())
.andDo(print());
请求的打印输出:
MockHttpServletRequest:
HTTP Method = GET
Request URI = /admin/config
Parameters = {names=[]}
Headers = {Accept=[application/json]}