I tried to get it by:
private static ApplicationContext applicationContext;
@Autowired
public static void setApplicationContext(ApplicationContext applicationContext) {
AuditorTest.applicationContext = applicationContext;
}
But it doesn't work as all other attempts.
How to autowire static ApplicationContext
?
You can't autowire spring beans on static
methods. You've to make it an instance method instead, and let it assign the value to static
variable (that will work fine):
@Autowired
public void setApplicationContext(ApplicationContext applicationContext) {
AuditorTest.applicationContext = applicationContext;
}
But I don't think this is what you want. I guess you should annotate the test class with SpringJUnitRunner
, and @ContextConfiguration
, and then you'll be able to autowire the ApplicationContext
there:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(...) // configuration location
public class TestClass {
@Autowired
private ApplicationContext context;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With