Hemann Anthony Kim
Hemann Anthony Kim

Reputation: 53

Is there any other way to put parameter of MockMVC?

I am currently testing api endpoints using Spring MockMvc and junit. It just works fine with the following code.

    public void testGetMethod(String url, String locale, String empKey, String accessToken) throws Exception {
    mockMvc.perform(get(url).param("locale", locale).param("empKey", empKey).param("accessToken", accessToken))
           .andDo(print())
           .andExpect(status().isOk());
}

But the thing is when I am trying to modify this code as follows (for setting parameters with .properties file later), I am getting 400 code with message, "Required String parameter 'locale' is not present".

    public void testGetMethod_param(String url, String locale, String empKey, String accessToken) throws Exception {
    MultiValueMap<String, Object> paraMap =new LinkedMultiValueMap<>();
    paraMap.add("locale", locale);
    paraMap.add("empKey", empKey);
    paraMap.add("accessToken", accessToken);
    mockMvc.perform(get(url))
    .andDo(print())
    .andExpect(status().isOk());
}

Can anybody point out what I'm doing wrong here?

Upvotes: 0

Views: 7413

Answers (1)

user54321
user54321

Reputation: 632

You need to add the paraMap to the get request.

 mockMvc.perform(get(url).params(paraMap))
    .andDo(print())
    .andExpect(status().isOk());

Upvotes: 1

Related Questions