MockMvc adds quotes at the ends of string when passed in param()
in request builder like following
// initialization of mockMvc
mockMvc = MockMvcBuilders.webAppContextSetup(context).build();
ObjectNode tweets = ((ObjectNode) result.getRequest().getAttribute("tweets"));
String query = tweets.get("query").toString();
String nextToken = tweets.get("meta").get("next_token").toString();
mockMvc.perform(MockMvcRequestBuilders.post("/next")
.param("query", query)
.param("next_token", nextToken)
.accept(MediaType.APPLICATION_JSON))
.andExpect(status().isOk())
.andExpect(jsonPath("$.isError", is("N")))
.andReturn();
if query is "#GoGreen"
and next_token is "wefw234234ewf234"
are received in controller is
query = "\"#GoGreen\""
and next_token = "\"wefw234234ewf234\""
@PostMapping("/next") @ResponseBody
public ResponseEntity<Object> nextPageTrendingTweets(@RequestParam("query") String query,
@RequestParam("next_token") String nextToken)
Maybe I'm missing something when initializing mockMvc
. I searched about this problem but couldn't find any solution.
The problem is not with mockMvc but it is with how you're extracting data from
ObjectNode
"tweets". You've used.toString()
method to get string data. However, it is incorrect becauseObjectNode.get()
method returns object and.toString()
converts it into string representation that's why extra quotes. So the solution of the problem is useObjectNode.get().asText()
method which will return that exact string in the object without any modification or conversion.