Spring dependency injection into Spring TestExecutionListeners not working

前端 未结 2 404
长发绾君心
长发绾君心 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:35

    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.

提交回复
热议问题