Spring dependency injection into Spring TestExecutionListeners not working

前端 未结 2 392
长发绾君心
长发绾君心 2021-01-05 06:22

How can I use Spring dependency injection into a TestExecutionListener class I wrote extending AbstractTestExecutionListener?

Spring DI does not seem to work with Te

2条回答
  •  北海茫月
    2021-01-05 06:31

    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.

提交回复
热议问题