MockMvc integration
如果您希望使用 AssertJ 支持,但已投资于原始的 MockMvc
API,则 MockMvcTester
提供了多种与其集成的方法。
If you want to use the AssertJ support but have invested in the original MockMvc
API, MockMvcTester
offers several ways to integrate with it.
如果您有自己的 RequestBuilder
实现,可以使用 perform
触发请求处理。下面的示例展示了如何使用原始 API 制作查询:
If you have your own RequestBuilder
implementation, you can trigger the processing
of the request using perform
. The example below showcases how the query can be
crafted with the original API:
// Static import on MockMvcRequestBuilders.get
assertThat(mockMvc.perform(get("/hotels/{id}", 42)))
.hasStatusOk();
同样,如果您制作了自定义匹配器,可在 MockMvc
的 .andExpect
特性中使用,则可以通过 .matches
使用它们。在下面的示例中,我们重写了前面的示例,以使用 MockMvc
提供的 ResultMatcher
实现断言状态:
Similarly, if you have crafted custom matchers that you use with the .andExpect
feature
of MockMvc
you can use them via .matches
. In the example below, we rewrite the
preceding example to assert the status with the ResultMatcher
implementation that
MockMvc
provides:
// Static import on MockMvcResultMatchers.status
assertThat(mockMvc.get().uri("/hotels/{id}", 42))
.matches(status().isOk());
MockMvc
还定义了一个 ResultHandler
合约,它允许你在 MvcResult
上执行任意操作。如果你已实现此合约,则可以使用 .apply
调用它。
MockMvc
also defines a ResultHandler
contract that lets you execute arbitrary actions
on MvcResult
. If you have implemented this contract you can invoke it using .apply
.