Maybe it is this bug of json-path: https://code.google.com/p/json-path/issues/detail?id=24 Try to use json-path 0.9.
Junit Test cases for Spring MVC controller to check @PathVariable parameters in @RequestMapping
-
17-07-2023 - |
Question
I have following controller code for which I have to write Junit test case.
@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);
}
I'm writing the Junit test case for the same as
@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));
}
Here I want to test the params passed in rest url path variable with the json sent in ResponEntity.
Tried to do so with
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)));
But its giving error as
java.lang.ClassCastException: net.minidev.json.JSONObject cannot be cast to java.util.List
Can anyone please suggest how to do this.
Solution
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow