【发布时间】:2014-05-07 15:46:31
【问题描述】:
我有以下控制器代码,我必须为其编写 Junit 测试用例。
@RequestMapping(value = "/cookbooks/{cookbook}/{version:.+}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
public @ResponseBody
ResponseEntity<String> getCookBookDetails(@PathVariable String cookbook, @PathVariable String version) {
return proxyController.getCookBookDetails(cookbook, version);
}
我正在编写与
相同的 Junit 测试用例@Test
public void getCookBookDetailsSuccessTest() throws Exception {
String cookbook = "{" + "\"cookbook\":{"
+ "\"version\": \"maven-1.1.0\","
+ "\"cookbook_name\": \"maven\"}" + "}";
when(proxyController.getCookBooks()).thenReturn(
new ResponseEntity<String>(cookbook, new HttpHeaders(),
HttpStatus.OK));
mockMvc.perform(get("/managment/cookbooks")
.param("cookbook", "maven")
.param("version", "1.1.0"))
.andExpect(status().isOk())
.andExpect(content().contentType(MediaType.APPLICATION_JSON_VALUE));
}
这里我想用ResponEntity中发送的json来测试rest url路径变量中传递的参数。
尝试使用
mockMvc.perform(
get("/managment/cookbooks").param("cookbook", "maven").param(
"version", "1.1.0"))
.andExpect(status().isOk())
.andExpect(content().contentType(MediaType.APPLICATION_JSON_VALUE))
.andExpect(jsonPath("$cookbook[0]['cookbook_name']", is(cookbook)));
但它给出的错误是
java.lang.ClassCastException:net.minidev.json.JSONObject 无法转换为 java.util.List
任何人都可以建议如何做到这一点。
【问题讨论】:
标签: spring spring-mvc junit