Configuring MockMvcTester
-
直接设置:直接指向要测试的控制器,并以编程方式配置 Spring MVC 基础设施。
-
通过 Spring 配置:使用 Spring 配置指向 Spring MVC 及其控制器基础设施。
“MockMvcTester”支持将 JSON 响应转换为域对象,前提是已注册相关的转换器。还可以提供现有的“MockMvc”实例以使用该实例创建“MockMvcTester”。
“MockMvcTester”可以通过两种方式之一进行设置。一是直接指向想要测试的控制器并以编程方式配置 Spring MVC 基础设施。第二种方法是使用 Spring 配置指向 Spring MVC 及其控制器基础设施。
MockMvcTester
can be setup in one of two ways. One is to point directly to the
controllers you want to test and programmatically configure Spring MVC infrastructure.
The second is to point to Spring configuration with Spring MVC and controller
infrastructure in it.
有关这两种模式的比较,请检查 Setup Options。 |
For a comparison of those two modes, check Setup Options. |
要为测试特定控制器设置“MockMvcTester”,请使用以下内容:
To set up MockMvcTester
for testing a specific controller, use the following:
-
Java
-
Kotlin
public class AccountControllerStandaloneTests {
private final MockMvcTester mockMvc = MockMvcTester.of(new AccountController());
// ...
}
class AccountControllerStandaloneTests {
val mockMvc = MockMvcTester.of(AccountController())
// ...
}
要通过 Spring 配置设置“MockMvcTester”,请使用以下内容:
To set up MockMvcTester
through Spring configuration, use the following:
-
Java
-
Kotlin
@SpringJUnitWebConfig(ApplicationWebConfiguration.class)
class AccountControllerIntegrationTests {
private final MockMvcTester mockMvc;
AccountControllerIntegrationTests(@Autowired WebApplicationContext wac) {
this.mockMvc = MockMvcTester.from(wac);
}
// ...
}
@SpringJUnitWebConfig(ApplicationWebConfiguration::class)
class AccountControllerIntegrationTests(@Autowired wac: WebApplicationContext) {
private val mockMvc = MockMvcTester.from(wac)
// ...
}
“MockMvcTester”可以将 JSON 响应主体或 JSONPath 表达式的结果转换为你的某个域对象,只要注册了相关的“HttpMessageConverter”。
MockMvcTester
can convert the JSON response body, or the result of a JSONPath expression,
to one of your domain object as long as the relevant HttpMessageConverter
is registered.
如果使用 Jackson 将内容序列化为 JSON,以下示例将注册转换器:
If you use Jackson to serialize content to JSON, the following example registers the converter:
-
Java
-
Kotlin
@SpringJUnitWebConfig(ApplicationWebConfiguration.class)
class AccountControllerIntegrationTests {
private final MockMvcTester mockMvc;
AccountControllerIntegrationTests(@Autowired WebApplicationContext wac) {
this.mockMvc = MockMvcTester.from(wac).withHttpMessageConverters(
List.of(wac.getBean(AbstractJackson2HttpMessageConverter.class)));
}
// ...
}
@SpringJUnitWebConfig(ApplicationWebConfiguration::class)
class AccountControllerIntegrationTests(@Autowired wac: WebApplicationContext) {
private val mockMvc = MockMvcTester.from(wac).withHttpMessageConverters(
listOf(wac.getBean(AbstractJackson2HttpMessageConverter::class.java)))
// ...
}
以上内容假定已将转换器注册为 Bean。 |
The above assumes the converter has been registered as a Bean. |
最后,如果有一个现成的“MockMvc”实例,可以通过使用“create”工厂方法提供要使用的“MockMvc”实例来创建一个“MockMvcTester”。
Finally, if you have a MockMvc
instance handy, you can create a MockMvcTester
by
providing the MockMvc
instance to use using the create
factory method.