是否可以在使用 MockMvc 时添加断言消息?
Is it possible to add an assertion message when using MockMvc?
大多数时候,我们不是在普通的 JUnit
断言中添加注释,而是向断言中添加一条消息,以解释为什么这是断言:
Person p1 = new Person("Bob");
Person p2 = new Person("Bob");
assertEquals(p1, p2, "Persons with the same name should be equal.");
现在,当涉及到在 Spring 引导网络环境中进行端点测试时,我最终得到的是:
// Bad request because body not posted
mockMvc.perform(post("/postregistration")).andExpect(status().isBadRequest());
// Body posted, it should return OK
mockMvc.perform(post("/postregistration").content(toJson(registrationDto))
.andExpect(status().isOk()));
有没有办法去掉注释并为这种断言添加消息?所以,当测试失败时,我会看到消息。
我发现 assertDoesNotThrow
响应因此改善了情况(根据我的要求):
assertDoesNotThrow(() -> {
mockMvc.perform(post("/postregistration")).andExpect(status().isBadRequest());
}, "Bad Request expected since body not posted.");
您可以提供自定义 ResultMatcher:
mockMvc.perform(post("/postregistration")
.content(toJson(registrationDto))
.andExpect(result -> assertEquals("Body posted, it should return OK", HttpStatus.OK.value() , result.getResponse().getStatus())))
mockMvc.perform(post("/postregistration"))
.andExpect(result -> assertEquals("Bad request because body not posted", HttpStatus.BAD_REQUEST.value(), result.getResponse().getStatus()));
说明:
截至今天,方法 .andExpect()
只接受一个 ResultMatcher。当您使用 .andExpect(status().isOk())
时,class StatusResultMatchers 将以这种方式创建一个 ResultMatcher:
public class StatusResultMatchers {
//...
public ResultMatcher isOk() {
return matcher(HttpStatus.OK);
}
//...
private ResultMatcher matcher(HttpStatus status) {
return result -> assertEquals("Status", status.value(), result.getResponse().getStatus());
}
}
如您所见,该消息被硬编码为“状态”,并且没有其他内置方法来配置它。因此,尽管提供自定义 ResultMatcher 有点冗长,但目前可能是使用 mockMvc 的唯一可行方法。
大多数时候,我们不是在普通的 JUnit
断言中添加注释,而是向断言中添加一条消息,以解释为什么这是断言:
Person p1 = new Person("Bob");
Person p2 = new Person("Bob");
assertEquals(p1, p2, "Persons with the same name should be equal.");
现在,当涉及到在 Spring 引导网络环境中进行端点测试时,我最终得到的是:
// Bad request because body not posted
mockMvc.perform(post("/postregistration")).andExpect(status().isBadRequest());
// Body posted, it should return OK
mockMvc.perform(post("/postregistration").content(toJson(registrationDto))
.andExpect(status().isOk()));
有没有办法去掉注释并为这种断言添加消息?所以,当测试失败时,我会看到消息。
我发现 assertDoesNotThrow
响应因此改善了情况(根据我的要求):
assertDoesNotThrow(() -> {
mockMvc.perform(post("/postregistration")).andExpect(status().isBadRequest());
}, "Bad Request expected since body not posted.");
您可以提供自定义 ResultMatcher:
mockMvc.perform(post("/postregistration")
.content(toJson(registrationDto))
.andExpect(result -> assertEquals("Body posted, it should return OK", HttpStatus.OK.value() , result.getResponse().getStatus())))
mockMvc.perform(post("/postregistration"))
.andExpect(result -> assertEquals("Bad request because body not posted", HttpStatus.BAD_REQUEST.value(), result.getResponse().getStatus()));
说明:
截至今天,方法 .andExpect()
只接受一个 ResultMatcher。当您使用 .andExpect(status().isOk())
时,class StatusResultMatchers 将以这种方式创建一个 ResultMatcher:
public class StatusResultMatchers {
//...
public ResultMatcher isOk() {
return matcher(HttpStatus.OK);
}
//...
private ResultMatcher matcher(HttpStatus status) {
return result -> assertEquals("Status", status.value(), result.getResponse().getStatus());
}
}
如您所见,该消息被硬编码为“状态”,并且没有其他内置方法来配置它。因此,尽管提供自定义 ResultMatcher 有点冗长,但目前可能是使用 mockMvc 的唯一可行方法。