TestExecutionListeners 注释阻止 spring beans 被连接

TestExecutionListeners annotation prevents spring beans being wired in

cassandra-unit with spring example 之后,我发现 spring bean 没有连接到测试 class 中,导致空指针异常。我试图尽量减少问题,发现它可能不是 Cassandra 部分,而是 @TestExecutionListeners 注释的存在,以及 AbstractTestExecutionListener 扩展 class.

org.springframework:spring-core:4.2.0.RELEASE (Also fails with 3.2.14.RELEASE).
org.springframework:spring-test:4.2.0.RELEASE
junit.junit:4.11

JVM vendor/version: Java HotSpot(TM) 64-Bit Server VM/1.8.0_40
MAC OS X 10.10.5

我的测试类看起来像:

@RunWith(SpringJUnit4ClassRunner.class)
@TestExecutionListeners({ AppTestListener.class }) <-- OK when removed
@ContextConfiguration(classes = { TestConfiguration.class })

public class MyTest {
  @Autowired
  private MyService myService;

  @Test
  public void testMyService() {
    Assert.assertNotNull(myService);
    Assert.assertEquals("didit", myService.doIt());
  }
}

AppTestListener:

public class AppTestListener extends AbstractTestExecutionListener {
  @Override
  public void beforeTestMethod(TestContext testContext) throws Exception {
    System.out.println("test");
  }
}

配置 class 没有什么特别的(而且配置 xml 也失败):

@Configuration
public class TestConfiguration {
  @Bean
  public MyService myService() {
    return new MyService(); 
  }
}

当我删除 MyTest 中的 @TestExecutionListeners 注释时,测试按预期完成,但保留该注释会使单元测试在 assertNotNull 上失败。 发生了什么事?

对于初学者来说,不幸的是,cassandra-unit 示例并不是一个很好的示例,因为它会导致您遇到的问题。

When I remove the @TestExecutionListeners annotation in MyTest the test finishes as expected, but leaving that annotation makes the unittest fail on the assertNotNull. What is happening?

当您在不扩展任何其他用 @TestExecutionListeners 注释的测试 class 上声明 @TestExecutionListeners(AppTestListener.class) 时,您实际上是在告诉 Spring加载 你的 AppTestListener,当你真的想使用 AppTestListener 和来自 Spring 的默认侦听器(例如,DependencyInjectionTestExecutionListener 添加了对来自 ApplicationContext).

的 bean 依赖注入的支持

有关详细信息,请阅读 Spring 参考手册的整个 TestExecutionListener configuration 部分。

以下是解决问题的方法。

Spring Framework 4.1

之前
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration
@TestExecutionListeners({
  CassandraUnitTestExecutionListener.class,
  DependencyInjectionTestExecutionListener.class,
  DirtiesContextTestExecutionListener.class,
  TransactionalTestExecutionListener.class
})
@CassandraUnit
public class MyCassandraUnitTest {

  @Test
  public void xxx_xxx() {
  }
}

Spring Framework 4.1

之后
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration
@TestExecutionListeners(
  listeners = CassandraUnitTestExecutionListener.class,
  mergeMode = MERGE_WITH_DEFAULTS
)
@CassandraUnit
public class MyCassandraUnitTest {

  @Test
  public void xxx_xxx() {
  }
}

此致,

Sam(Spring TestContext Framework 的作者)

p.s。我创建了一个 issue for Cassandra Unit 以便他们在示例中解决这个问题。