【问题标题】:TestExecutionListeners annotation prevents spring beans being wired inTestExecutionListeners 注释可防止连接 spring bean
【发布时间】:2015-08-29 13:24:19
【问题描述】:

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

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

我的 TestClass 看起来像:

@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");
  }
}

配置类没有什么特别的(配置xml也失败了):

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

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

【问题讨论】:

    标签: java spring spring-test


    【解决方案1】:

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

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

    当你在一个不扩展任何其他用@TestExecutionListeners 注释的测试类的测试类上声明@TestExecutionListeners(AppTestListener.class) 时,你实际上是在告诉Spring 在你真正想要的时候加载你的AppTestListenerAppTestListener 与 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 框架的作者

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-09-22
      • 2013-02-07
      • 1970-01-01
      • 2020-12-06
      • 1970-01-01
      • 2015-12-14
      • 1970-01-01
      相关资源
      最近更新 更多