@Autowired ApplicationContext
作为实现接口的替代方法,您可以注入
测试类的应用程序上下文,通过对任一的注释
字段或 setter 方法,如以下示例所示:ApplicationContextAware @Autowired
@SpringJUnitConfig
class MyTest {
@Autowired (1)
ApplicationContext applicationContext;
// class body...
}
@SpringJUnitConfig
class MyTest {
@Autowired (1)
lateinit var applicationContext: ApplicationContext
// class body...
}
同样,如果您的测试配置为加载 ,则可以注入
将 Web 应用程序上下文添加到测试中,如下所示:WebApplicationContext
@SpringJUnitWebConfig (1)
class MyWebAppTest {
@Autowired (2)
WebApplicationContext wac;
// class body...
}
1 |
配置 .WebApplicationContext |
2 |
注入 .WebApplicationContext |
@SpringJUnitWebConfig (1)
class MyWebAppTest {
@Autowired (2)
lateinit var wac: WebApplicationContext
// class body...
}
1 |
配置 .WebApplicationContext |
2 |
注入 .WebApplicationContext |
依赖注入由 提供 ,默认配置
(请参阅测试夹具的依赖注入)。@Autowired DependencyInjectionTestExecutionListener
|