How can I use Spring dependency injection into a TestExecutionListener class I wrote extending AbstractTestExecutionListener?
Spring DI does not seem to work with Te
In the case of Spring Boot 2 using
estContext.getApplicationContext()
.getAutowireCapableBeanFactory()
.autowireBean(this)
was triggering the creation of the Spring context before the @SpringBootTest
base class was created. This missed then some critical configuration parameters in my case. I had to use testContext.getApplicationContext().getBean(
in beforeTestClass
for getting a bean instance.
Just add autowiring for the whole TestExecutionListener.
@Override
public void beforeTestClass(TestContext testContext) throws Exception {
testContext.getApplicationContext()
.getAutowireCapableBeanFactory()
.autowireBean(this);
// your code that uses autowired fields
}
Check sample project in github.