定义预期
你可以通过在执行请求后附加一个或多个 andExpect(..)
调用来定义期望,如下例所示。一旦有一个期望失败,其他期望将不会被断言。
- Java
- Kotlin
// static import of MockMvcRequestBuilders.* and MockMvcResultMatchers.*
mockMvc.perform(get("/accounts/1")).andExpect(status().isOk());
import org.springframework.test.web.servlet.get
mockMvc.get("/accounts/1").andExpect {
status { isOk() }
}
你可以通过在执行请求后附加 andExpectAll(..)
来定义多个期望,如下例所示。与 andExpect(..)
不同,andExpectAll(..)
保证所有提供的期望都会被断言,并且所有失败都会被跟踪和报告。
- Java
- Kotlin
// static import of MockMvcRequestBuilders.* and MockMvcResultMatchers.*
mockMvc.perform(get("/accounts/1")).andExpectAll(
status().isOk(),
content().contentType("application/json;charset=UTF-8"));
import org.springframework.test.web.servlet.get
mockMvc.get("/accounts/1").andExpectAll {
status { isOk() }
content { contentType(APPLICATION_JSON) }
}
MockMvcResultMatchers.*
提供了许多期望值,其中一些还进一步嵌套了更详细的期望值。
期望可以分为两大类。第一类断言验证响应的属性(例如,响应状态、响应头和内容)。这些是需要断言的最重要结果。
第二类断言超出了响应的范围。这些断言允许你检查 Spring MVC 的特定方面,例如处理请求的控制器方法、是否引发并处理了异常、模型的内容是什么、选择了哪个视图、添加了哪些 flash 属性等等。它们还允许你检查 Servlet 的特定方面,例如请求和会话属性。
以下测试断言绑定或验证失败:
- Java
- Kotlin
mockMvc.perform(post("/persons"))
.andExpect(status().isOk())
.andExpect(model().attributeHasErrors("person"));
import org.springframework.test.web.servlet.post
mockMvc.post("/persons").andExpect {
status { isOk() }
model {
attributeHasErrors("person")
}
}
很多时候,在编写测试时,将执行请求的结果转储出来是非常有用的。你可以按照以下方式操作,其中 print()
是从 MockMvcResultHandlers
静态导入的:
- Java
- Kotlin
mockMvc.perform(post("/persons"))
.andDo(print())
.andExpect(status().isOk())
.andExpect(model().attributeHasErrors("person"));
import org.springframework.test.web.servlet.post
mockMvc.post("/persons").andDo {
print()
}.andExpect {
status { isOk() }
model {
attributeHasErrors("person")
}
}
只要请求处理不会导致未处理的异常,print()
方法就会将所有可用的结果数据打印到 System.out
。此外,还有一个 log()
方法和两个额外的 print()
方法变体,其中一个接受 OutputStream
,另一个接受 Writer
。例如,调用 print(System.err)
会将结果数据打印到 System.err
,而调用 print(myWriter)
则会将结果数据打印到自定义的写入器中。如果你希望记录结果数据而不是打印,你可以调用 log()
方法,该方法会将结果数据作为单个 DEBUG
消息记录在 org.springframework.test.web.servlet.result
日志类别下。
在某些情况下,您可能希望直接访问结果并验证某些无法通过其他方式验证的内容。这可以通过在所有其他期望之后追加 .andReturn()
来实现,如下例所示:
- Java
- Kotlin
MvcResult mvcResult = mockMvc.perform(post("/persons")).andExpect(status().isOk()).andReturn();
// ...
var mvcResult = mockMvc.post("/persons").andExpect { status { isOk() } }.andReturn()
// ...
如果所有测试都重复相同的预期,你可以在构建 MockMvc
实例时一次性设置公共预期,如下例所示:
- Java
- Kotlin
standaloneSetup(new SimpleController())
.alwaysExpect(status().isOk())
.alwaysExpect(content().contentType("application/json;charset=UTF-8"))
.build()
// Not possible in Kotlin until {kotlin-issues}/KT-22208 is fixed
请注意,常见的预期总是会被应用,除非创建单独的 MockMvc
实例,否则无法覆盖这些预期。
当 JSON 响应内容包含使用 Spring HATEOAS 创建的超媒体链接时,您可以使用 JsonPath 表达式来验证生成的链接,如下例所示:
- Java
- Kotlin
mockMvc.perform(get("/people").accept(MediaType.APPLICATION_JSON))
.andExpect(jsonPath("$.links[?(@.rel == 'self')].href").value("http://localhost:8080/people"));
mockMvc.get("/people") {
accept(MediaType.APPLICATION_JSON)
}.andExpect {
jsonPath("$.links[?(@.rel == 'self')].href") {
value("http://localhost:8080/people")
}
}
当 XML 响应内容包含使用 Spring HATEOAS 创建的超媒体链接时,您可以使用 XPath 表达式来验证生成的链接:
- Java
- Kotlin
Map<String, String> ns = Collections.singletonMap("ns", "http://www.w3.org/2005/Atom");
mockMvc.perform(get("/handle").accept(MediaType.APPLICATION_XML))
.andExpect(xpath("/person/ns:link[@rel='self']/@href", ns).string("http://localhost:8080/people"));
val ns = mapOf("ns" to "http://www.w3.org/2005/Atom")
mockMvc.get("/handle") {
accept(MediaType.APPLICATION_XML)
}.andExpect {
xpath("/person/ns:link[@rel='self']/@href", ns) {
string("http://localhost:8080/people")
}
}