我有简单的积分测试
@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()的一些技巧,但都不起作用。
这是一种更适合生产的方式,如果你可能有大的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"
所以这就是它有效的证明!