|
@Autowired ApplicationContext
As an alternative to implementing the ApplicationContextAware interface, you can inject
the application context for your test class through the @Autowired annotation on either
a field or setter method, as the following example shows:spring-doc.cn
@SpringJUnitConfig
class MyTest {
@Autowired (1)
ApplicationContext applicationContext;
// class body...
}
| 1 |
Injecting the ApplicationContext. |
@SpringJUnitConfig
class MyTest {
@Autowired (1)
lateinit var applicationContext: ApplicationContext
// class body...
}
| 1 |
Injecting the ApplicationContext. |
Similarly, if your test is configured to load a WebApplicationContext, you can inject
the web application context into your test, as follows:spring-doc.cn
@SpringJUnitWebConfig (1)
class MyWebAppTest {
@Autowired (2)
WebApplicationContext wac;
// class body...
}
| 1 |
Configuring the WebApplicationContext. |
| 2 |
Injecting the WebApplicationContext. |
@SpringJUnitWebConfig (1)
class MyWebAppTest {
@Autowired (2)
lateinit var wac: WebApplicationContext
// class body...
}
| 1 |
Configuring the WebApplicationContext. |
| 2 |
Injecting the WebApplicationContext. |
|