【问题标题】:Why TestExecutionListener does not work as a bean?为什么 TestExecutionListener 不能作为 bean 工作?
【发布时间】:2015-09-13 18:04:38
【问题描述】:

为什么ApplicationListener 可以作为bean 工作,而TestExecutionListener 不能?

以下代码显示没有来自MyListener1 的任何消息,因为TestExecutionListener 应该通过@TestExecutionListeners 注册。

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = TestExecutionListenerTry._Config.class)
public class TestExecutionListenerTry {

    public static class Bean1 {
    }

    public static class Bean2 {
    }

    public static class MyListener1 implements TestExecutionListener {

        @Override
        public void beforeTestClass(TestContext testContext) throws Exception {
            System.out.println("beforeTestClass " + testContext.toString());
        }

        @Override
        public void prepareTestInstance(TestContext testContext) throws Exception {
            System.out.println("prepareTestInstance " + testContext.toString());
        }

        @Override
        public void beforeTestMethod(TestContext testContext) throws Exception {
            System.out.println("beforeTestMethod " + testContext.toString());
        }

        @Override
        public void afterTestMethod(TestContext testContext) throws Exception {
            System.out.println("afterTestMethod " + testContext.toString());
        }

        @Override
        public void afterTestClass(TestContext testContext) throws Exception {
            System.out.println("afterTestClass " + testContext.toString());
        }
    }

    public static class MyListener2 implements ApplicationListener<ContextRefreshedEvent> {
        @Override
        public void onApplicationEvent(ContextRefreshedEvent event) {
            System.out.println("ContextRefreshedEvent " + event.toString());
        }
    }

    @Configuration
    public static class _Config {

        @Bean
        public Bean1 bean1() {
            return new Bean1();
        }

        @Bean
        public Bean2 bean2() {
            return new Bean2();
        }

        @Bean
        public MyListener1 myListener1() {
            return new MyListener1();
        }

        @Bean
        public MyListener2 myListener2() {
            return new MyListener2();
        }
    }


    @Test
    public void test1() {
        System.out.println("test1()");
    }

    @Test
    public void test2() {
        System.out.println("test2()");
    }


}

为什么会有这样的设计差异?

有没有可以监听测试的bean?

【问题讨论】:

    标签: java spring junit4 spring-test


    【解决方案1】:

    ApplicationContext,bean 的容器,只知道如何生成和暴露 bean。这或多或少是其功能的限制。它对测试或测试环境一无所知。

    ApplicationListener 可以声明为一个 bean,因为 ApplicationContext 定义了监听器可以观察到的其生命周期中的各个阶段(不管它在哪里使用)。

    TestExecutionListener 但是,仅在运行测试的上下文中才有用。这与ApplicationContext 所做的任何事情无关。换句话说,只有SpringJUnit4ClassRunner 关心这些侦听器,因为它运行测试方法。

    实际上,TestExecutionListener bean 可能已经被 SpringJUnit4ClassRunnerApplicationContext 中提取出来了。就我而言,这是一个关注点分离的问题。

    【讨论】:

      猜你喜欢
      • 2011-07-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-01-13
      • 2011-05-11
      • 2012-01-15
      • 1970-01-01
      • 2012-08-11
      相关资源
      最近更新 更多