MockMVC adds additional quotes with string in request param with post request

713 views Asked by At

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.

2

There are 2 answers

0
Raj Sojitra On BEST ANSWER

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 because ObjectNode.get() method returns object and .toString() converts it into string representation that's why extra quotes. So the solution of the problem is use ObjectNode.get().asText() method which will return that exact string in the object without any modification or conversion.

1
chrylis -cautiouslyoptimistic- On

The fragment part (#foo) lives entirely inside the browser and is not passed in HTTP requests. Don't send it at all.