我有以下控制器代码,我必须编写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));
}
这里我想测试在rest urnt路径变量中传递的参数,并在ResponEntity中发送json。
试图用
这样做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
任何人都可以建议如何做到这一点。
答案 0 :(得分:0)
也许是json-path的这个bug:https://code.google.com/p/json-path/issues/detail?id=24尝试使用json-path 0.9。