简体   繁体   English

如何在单元测试中使用自定义Spring范围(SpringJUnit4ClassRunner)

[英]How to use custom Spring scope with unit tests (SpringJUnit4ClassRunner)

I am using JUnit tests with Spring configuration defined in a class annotated with @Configuration in my JUnit Test. 我使用JUnit测试,在与注释的类定义Spring配置@Configuration在我的JUnit测试。 The tests looks like this: 测试如下所示:

@ContextConfiguration(classes = MyConfiguration.class})
@RunWith(SpringJUnit4ClassRunner.class)
@DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_EACH_TEST_METHOD)
public class SomeIntegrationTest {

    @Autowired
    private MyConfiguration myConfiguration;

    @Test
    public void someTest() throws Exception {
       myConfiguration.myBean();
    }
}

In MyConfiguration , I would like to use Spring scope SimpleThreadScope : MyConfiguration ,我想使用Spring作用域SimpleThreadScope

@Configuration
public class MyConfiguration {

    @Bean
    @Scope("thread")
    public MyBean myBean() {
        return new MyBean();
    }
}

When I run the test, the scope is not registered, though. 但是,当我运行测试时,范围未注册。 I get 我懂了

java.lang.IllegalStateException: Failed to load ApplicationContext
...
Caused by: java.lang.IllegalStateException: No Scope registered for scope 'thread'

I am aware how a custom scope can be registered programatically: context.getBeanFactory().registerScope("thread", new SimpleThreadScope()); 我知道如何以编程方式注册自定义范围: context.getBeanFactory().registerScope("thread", new SimpleThreadScope());
and I would like to avoid using XML Spring configuration. 而且我想避免使用XML Spring配置。

Is there any way, how can I register the custom scope in the unit test? 有什么办法,如何在单元测试中注册自定义范围?

Check this execution listener: 检查此执行侦听器:

public class WebContextTestExecutionListener extends
            AbstractTestExecutionListener {

        @Override
        public void prepareTestInstance(TestContext testContext) throws Exception {

            if (testContext.getApplicationContext() instanceof GenericApplicationContext) {
                GenericApplicationContext context = (GenericApplicationContext) testContext.getApplicationContext();
                ConfigurableListableBeanFactory beanFactory = context
                        .getBeanFactory();
                Scope requestScope = new SimpleThreadScope();
                beanFactory.registerScope("request", requestScope);
                Scope sessionScope = new SimpleThreadScope();
                beanFactory.registerScope("session", sessionScope);
                Scope threadScope= new SimpleThreadScope();
                beanFactory.registerScope("thread", threadScope);
            }
        }
    }

in the test you can put this 在测试中,你可以把这个

    @ContextConfiguration(classes = MyConfiguration.class})
    @RunWith(SpringJUnit4ClassRunner.class)
    @DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_EACH_TEST_METHOD)
    @TestExecutionListeners( { WebContextTestExecutionListener.class})
    public class UserSpringIntegrationTest {

    @Autowired
    private UserBean userBean;

    //All the test methods
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在SpringJUnit4ClassRunner中使用spring @Conditional - spring @Conditional in SpringJUnit4ClassRunner 在Eclipse 4.3中使用@ContextConfiguration和SpringJUnit4ClassRunner进行的春季测试似乎很慢 - Spring tests with @ContextConfiguration and SpringJUnit4ClassRunner in Eclipse 4.3 seem slow 如何通过SpringJUnit4ClassRunner使用RunNotifier或RunListener - How to use RunNotifier or RunListener using SpringJUnit4ClassRunner SpringJUnit4ClassRunner中的@AfterClass(如何在拆卸中使用bean) - @AfterClass in SpringJUnit4ClassRunner (how to use beans in teardown) SpringJUnit4ClassRunner在Spring5中损坏了吗? - SpringJUnit4ClassRunner broken in Spring5? 如何使用Parametrized运行JUnit SpringJUnit4ClassRunner? - How to run JUnit SpringJUnit4ClassRunner with Parametrized? 如何增加SpringJUnit4ClassRunner使用的类路径? - How to augment the classpath used by SpringJUnit4ClassRunner? SpringJUnit4ClassRunner测试-通过ant而不是IDE进行工作 - SpringJUnit4ClassRunner tests — work from ant, not from IDE 使用SpringJUnit4ClassRunner仅使用@Component和@Autowired进行单元测试 - Unit testing with SpringJUnit4ClassRunner using only @Component and @Autowired Spring - SpringJUnit4ClassRunner 仍然使用 LOG4J 1 而不是 2? - Spring - SpringJUnit4ClassRunner still using LOG4J 1 instead of 2?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM