我正在使用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)
有没有其他方法来测试这种情况?
答案 0 :(得分:1)
我认为将您的值设置为空字符串是有效的。
我的控制器
@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]}