使用TestExecutionListener时,Spring测试注入不起作用

nan*_*sen 14 java spring spring-test liquibase

我想结合使用自定义TestExecutionListenerSpringJUnit4ClassRunner我的测试数据库上运行Liquibase架构设置.我的TestExecutionListener工作正常,但是当我在我的类上使用注释时,测试中的DAO注入不再起作用,至少实例为null.

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "file:src/main/webapp/WEB-INF/applicationContext-test.xml" })
@TestExecutionListeners({ LiquibaseTestExecutionListener.class })
@LiquibaseChangeSet(changeSetLocations={"liquibase/v001/createTables.xml"})
public class DeviceDAOTest {

    ...

    @Inject
    DeviceDAO deviceDAO;

    @Test
    public void findByCategory_categoryHasSubCategories_returnsAllDescendantsDevices() {
        List<Device> devices = deviceDAO.findByCategory(1); // deviceDAO null -> NPE
        ...
    }
}
Run Code Online (Sandbox Code Playgroud)

听众很简单:

public class LiquibaseTestExecutionListener extends AbstractTestExecutionListener {

    @Override
    public void beforeTestClass(TestContext testContext) throws Exception {
        final LiquibaseChangeSet annotation = AnnotationUtils.findAnnotation(testContext.getTestClass(),
                LiquibaseChangeSet.class);
        if (annotation != null) {
            executeChangesets(testContext, annotation.changeSetLocations());
        }
    }

    private void executeChangesets(TestContext testContext, String[] changeSetLocation) throws SQLException,
            LiquibaseException {
        for (String location : changeSetLocation) {
            DataSource datasource = testContext.getApplicationContext().getBean(DataSource.class);
            DatabaseConnection database = new JdbcConnection(datasource.getConnection());
            Liquibase liquibase = new Liquibase(location, new FileSystemResourceAccessor(), database);
            liquibase.update(null);
        }
    }

}
Run Code Online (Sandbox Code Playgroud)

日志中没有错误,只是NullPointerException在我的测试中.我没有看到我的使用如何TestExecutionListener影响自动装配或注射.

nan*_*sen 23

我看了一下spring DEBUG日志,发现当我省略自己的TestExecutionListener时,Spring会设置一个DependencyInjectionTestExecutionListener.使用@TestExecutionListeners注释测试时,该侦听器会被覆盖.

所以我刚刚使用我的自定义添加了DependencyInjectionTestExecutionListener,一切正常:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "file:src/main/webapp/WEB-INF/applicationContext-test.xml" })
@TestExecutionListeners(listeners = { LiquibaseTestExecutionListener.class,
    DependencyInjectionTestExecutionListener.class })
@LiquibaseChangeSet(changeSetLocations = { "liquibase/v001/createTables.xml" })
public class DeviceDAOTest {
    ...
Run Code Online (Sandbox Code Playgroud)

更新:此处记录了行为.

...或者,您可以通过使用@TestExecutionListeners显式配置类并从侦听器列表中省略DependencyInjectionTestExecutionListener.class来完全禁用依赖项注入.

  • 值得补充的是,自Spring 4.1开始,`mergeMode = MergeMode.MERGE_WITH_DEFAULTS`可用于包含所有默认的TestExecutionListeners. (7认同)
  • 我很清楚自从我写这篇文章以来你引用的文字.;)但是......没有明确描述您遇到的场景.这就是为什么我建议你打开JIRA票来改进文档. (2认同)

Jea*_*ean 6

我建议考虑只做类似的事情:

@TestExecutionListeners(
        mergeMode =TestExecutionListeners.MergeMode.MERGE_WITH_DEFAULTS,
        listeners = {MySuperfancyListener.class}
)
Run Code Online (Sandbox Code Playgroud)

这样您就不需要知道需要哪些侦听器。我推荐这种方法是因为 SpringBoot 试图通过使用DependencyInjectionTestExecutionListener.class.