我目前正在使用 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 条带有消息的代码,“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());
}
谁能指出我在这里做错了什么?
您需要将 paraMap 添加到获取请求中。
mockMvc.perform(get(url).params(paraMap))
.andDo(print())
.andExpect(status().isOk());
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句