MockMvc
MockMvc 为测试 Spring MVC 应用程序提供支持。它执行完整的 Spring MVC 请求处理,但通过模拟请求和响应对象代替正在运行的服务器来进行。
MockMvc provides support for testing Spring MVC applications. It performs full Spring MVC request handling but via mock request and response objects instead of a running server.
MockMvc 本身可用于执行请求并使用 Hamcrest、或通过 MockMvcTester
来验证响应,后者提供一个使用 AssertJ 的流畅 API。最后,也可以通过 WebTestClient 使用 MockMvc,其中 MockMvc 作为服务器来处理请求。WebTestClient
的优势是,它可以选择使用更高级别的对象而不仅仅是原始数据,而且还可以切换到针对实时服务器的完整端到端 HTTP 测试以及使用相同的测试 API。
MockMvc can be used on its own to perform requests and verify responses responses using
Hamcrest, or through MockMvcTester
that provides a fluent API using AssertJ. Finally,
it can also be used through the WebTestClient where
MockMvc is plugged in as the server to handle requests with. The advantage of
WebTestClient
is the option to work with higher level objects instead of raw data as
well as the ability to switch to full, end-to-end HTTP tests against a live server and
use the same test API.