我有简单的积分测试
@Test
public void shouldReturnErrorMessageToAdminWhenCreatingUserWithUsedUserName() throws Exception {
mockMvc.perform(post("/api/users").header("Authorization", base64ForTestUser).contentType(MediaType.APPLICATION_JSON)
.content("{\"userName\":\"testUserDetails\",\"firstName\":\"xxx\",\"lastName\":\"xxx\",\"password\":\"xxx\"}"))
.andDo(print())
.andExpect(status().isBadRequest())
.andExpect(?);
}
在最后一行中,我想比较在响应体中接收到的字符串与预期的字符串
我得到的回答是:
MockHttpServletResponse:
Status = 400
Error message = null
Headers = {Content-Type=[application/json]}
Content type = application/json
Body = "Username already taken"
Forwarded URL = null
Redirected URL = null
尝试了content(), body()的一些技巧,但都不起作用。
摘自spring教程
mockMvc.perform(get("/" + userName + "/bookmarks/"
+ this.bookmarkList.get(0).getId()))
.andExpect(status().isOk())
.andExpect(content().contentType(contentType))
.andExpect(jsonPath("$.id", is(this.bookmarkList.get(0).getId().intValue())))
.andExpect(jsonPath("$.uri", is("http://bookmark.com/1/" + userName)))
.andExpect(jsonPath("$.description", is("A description")));
是可以从import static org.hamcrest.Matchers.*;
jsonPath可从导入静态org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
和jsonPath引用可以在这里找到
你可以调用andReturn()并使用返回的MvcResult对象来获取字符串形式的内容。
见下文:
MvcResult result = mockMvc.perform(post("/api/users").header("Authorization", base64ForTestUser).contentType(MediaType.APPLICATION_JSON)
.content("{\"userName\":\"testUserDetails\",\"firstName\":\"xxx\",\"lastName\":\"xxx\",\"password\":\"xxx\"}"))
.andDo(MockMvcResultHandlers.print())
.andExpect(status().isBadRequest())
.andReturn();
String content = result.getResponse().getContentAsString();
// do what you will
这是一种更适合生产的方式,如果你可能有大的json响应,那么你不必用json字符串来混乱你的测试文件,只需从静态资源文件夹加载它们并直接断言它们。
@Test
@DisplayName("invalid fields")
void invalidfields() throws Exception {
String request = getResourceFileAsString("test-data/http-request/invalid-fields.json");
String response_file_path = "test-data/http-response/error-messages/invalid-fields.json";
String expected_response = getResourceFileAsString(response_file_path);
mockMvc.perform(evaluateRulesOnData(TRACKING_ID.toString(), request))
.andExpect(status().isBadRequest())
.andExpect(content().json(expected_response));
}
从类路径加载测试文件的助手函数
public static String getResourceFileAsString(String fileName) throws IOException {
Resource resource = new ClassPathResource(fileName);
File file = resource.getFile();
return new String(Files.readAllBytes(file.toPath()));
}
预期的响应有一个包含列表中许多元素的数组,这些元素在每次测试运行期间都是随机顺序匹配的。
@Sotirios Delimanolis回答做的工作,但我正在寻找这个mockMvc断言中的比较字符串
就在这里
.andExpect(content().string("\"Username already taken - please try with different username\""));
当然,我的断言是失败的:
java.lang.AssertionError: Response content expected:
<"Username already taken - please try with different username"> but was:<"Something gone wrong">
因为:
MockHttpServletResponse:
Body = "Something gone wrong"
所以这就是它有效的证明!