Spring mvc 以对象列表作为请求参数的MockMvc集成测试
我正在使用SpringMVC开发REST服务,它将对象列表作为请求参数Spring mvc 以对象列表作为请求参数的MockMvc集成测试,spring-mvc,integration-testing,spring-test,mockmvc,spring-test-mvc,Spring Mvc,Integration Testing,Spring Test,Mockmvc,Spring Test Mvc,我正在使用SpringMVC开发REST服务,它将对象列表作为请求参数 @RequestMapping(value="/test", method=RequestMethod.PUT) public String updateActiveStatus(ArrayList<Test> testList, BindingResult result) throws Exception { if(testList.isEmpty()) {
@RequestMapping(value="/test", method=RequestMethod.PUT)
public String updateActiveStatus(ArrayList<Test> testList, BindingResult result) throws Exception {
if(testList.isEmpty()) {
throw new BadRequestException();
}
return null;
}
@RequestMapping(value=“/test”,method=RequestMethod.PUT)
公共字符串updateActiveStatus(ArrayList testList,BindingResult)引发异常{
if(testList.isEmpty()){
抛出新的BadRequestException();
}
返回null;
}
当我尝试对上述服务进行集成测试时,我无法发送请求参数中的测试对象列表
以下代码不适用于我
List<Test> testList = Arrays.asList(new Test(), new Test());
mockMvc.perform(put(ApplicationConstants.UPDATE_ACTIVE_STATUS)
.content(objectMapper.writeValueAsString(testList)))
.andDo(print());
List testList=Arrays.asList(new Test(),new Test());
mockMvc.perform(put(ApplicationConstants.UPDATE\u ACTIVE\u状态)
.content(objectMapper.writeValueAsString(testList)))
.andDo(print());
谁能帮我一下吗 使用Gson库将列表转换为json字符串,然后将该字符串放入内容中 还将带有方法参数的@RequestBody注释放在控制器中
公共字符串updateActiveStatus(@RequestBody ArrayList
@带有列表或数组的RequestParam
@RequestMapping(“/books”)
公共字符串书籍(@RequestParam List authors,
(模型){
model.addAttribute(“作者”,authors);
返回“books.jsp”;
}
@试验
当MultipleParameters\u thenList()引发异常时公共无效{
this.mockMvc.perform(get(“/books”)
.param(“作者”、“马丁”)
.param(“作者”、“托尔金”)
)
.andExpect(状态().isOk())
.andExpect(model().attribute(“authors”,包含(“martin”、“tolkien”));
}
@RequestMapping("/books")
public String books(@RequestParam List<String> authors,
Model model){
model.addAttribute("authors", authors);
return "books.jsp";
}
@Test
public void whenMultipleParameters_thenList() throws Exception {
this.mockMvc.perform(get("/books")
.param("authors", "martin")
.param("authors", "tolkien")
)
.andExpect(status().isOk())
.andExpect(model().attribute("authors", contains("martin","tolkien")));
}