您可以通过在 执行请求,如以下示例所示。一旦一个期望落空, 不会断言其他期望。andExpect(..)Spring中文文档

// 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(..)Spring中文文档

// 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中文文档

期望分为两大类。第一类断言验证 响应的属性(例如,响应状态、标头和内容)。 这些是要断言的最重要的结果。Spring中文文档

第二类断言超出了响应范围。这些断言可以让你 检查Spring MVC的具体方面,例如哪种控制器方法处理了 请求,是否引发并处理了异常,模型的内容是什么, 选择了哪些视图,添加了哪些 Flash 属性,等等。他们还让你 检查 Servlet 特定方面,例如请求和会话属性。Spring中文文档

以下测试断言绑定或验证失败:Spring中文文档

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()MockMvcResultHandlersSpring中文文档

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.outlog()print()OutputStreamWriterprint(System.err)System.errprint(myWriter)log()DEBUGorg.springframework.test.web.servlet.resultSpring中文文档

在某些情况下,您可能希望直接访问结果并验证以下内容 无法以其他方式进行验证。这毕竟可以通过附加来实现 其他期望,如以下示例所示:.andReturn()Spring中文文档

MvcResult mvcResult = mockMvc.perform(post("/persons")).andExpect(status().isOk()).andReturn();
// ...
var mvcResult = mockMvc.post("/persons").andExpect { status { isOk() } }.andReturn()
// ...

如果所有测试都重复相同的期望,则可以在以下情况下设置一次共同的期望 构建实例,如以下示例所示:MockMvcSpring中文文档

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

请注意,共同的期望始终适用,如果没有 创建一个单独的实例。MockMvcSpring中文文档

当 JSON 响应内容包含使用 Spring HATEOAS 创建的超媒体链接时,您可以验证 使用 JsonPath 表达式生成的链接,如以下示例所示:Spring中文文档

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 表达式生成的链接:Spring中文文档

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")
	}
}