我目前正在使用Spring MockMvc和junit测试api端点。 只需使用以下代码即可。
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());
}
但是事情是当我试图修改这段代码时 如下(用于稍后使用.properties文件设置参数), 我收到400条代码,并显示消息“不存在必需的字符串参数'locale'”。
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());
}
有人可以指出我在做什么错吗?
答案 0 :(得分:1)
您需要将paraMap添加到get请求。
mockMvc.perform(get(url).params(paraMap))
.andDo(print())
.andExpect(status().isOk());