【问题标题】:How do I identify when other JUnit tests steal my Spring Root Controller?如何识别其他 JUnit 测试何时窃取了我的 Spring Root Controller?
【发布时间】:2016-03-21 10:39:41
【问题描述】:

我有一个看起来像这样的 JUnit 测试 - 它是一个更大的应用程序的一部分。

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(loader = AnnotationConfigContextLoader.class, classes = { MyTestConfig.class })
public class MyHandlerInterceptorTest  {

    @Autowired
    private RequestMappingHandlerMapping requestMappingHandlerMapping;

    @Test
    public void myTest() throws Exception {
        MockHttpServletRequest request = new MockHttpServletRequest("GET",
            "/myrequest");
        HandlerExecutionChain handlerExecutionChain = requestMappingHandlerMapping.getHandler(request);
    }
}

当我单独运行测试时 - 它运行良好。

当我将它作为一组其他测试的一部分运行时 - 我收到错误

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'com.MyHandlerInterceptorTest  ': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping com.MyHandlerInterceptorTest.requestMappingHandlerMapping; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'requestMappingHandlerMapping' defined in class org.springframework.web.servlet.config.annotation.DelegatingWebMvcConfiguration: Initialization of bean failed; nested exception is org.springframework.context.ApplicationContextException: Cannot reinitialize with different application context: current one is [Root WebApplicationContext: startup date [XXX 2016]; root of context hierarchy], passed-in one is [org.springframework.context.support.GenericApplicationContext@4760457f: startup date [XXX 2016]; root of context hierarchy]
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:288)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1116)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireBeanProperties(AbstractAutowireCapableBeanFactory.java:376)
at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.injectDependencies(DependencyInjectionTestExecutionListener.java:110)
at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.prepareTestInstance(DependencyInjectionTestExecutionListener.java:75)
at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:313)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTest(SpringJUnit4ClassRunner.java:211)
....
Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping com.MyHandlerInterceptorTest.requestMappingHandlerMapping; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'requestMappingHandlerMapping' defined in class org.springframework.web.servlet.config.annotation.DelegatingWebMvcConfiguration: Initialization of bean failed; nested exception is org.springframework.context.ApplicationContextException: Cannot reinitialize with different application context: current one is [Root WebApplicationContext: startup date [XXX 2016]; root of context hierarchy], passed-in one is [org.springframework.context.support.GenericApplicationContext@4760457f: startup date [Fri Mar 18 11:01:19 EST 2016]; root of context hierarchy]
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:514)

似乎正在发生的事情是我的测试上下文被其他测试窃取了。我想知道如何识别何时发生并停止它,或者至少解决它。

这与其他问题不同,因为这是关于识别其他测试,而不是通过变通方法阻止。

我的问题是:我如何识别其他 JUnit 测试何时窃取了我的 Spring 根控制器?

【问题讨论】:

    标签: java spring unit-testing spring-mvc junit


    【解决方案1】:

    尝试将@WebAppConfiguration 添加到您的测试类,以便它加载WebApplicationContext 而不是一般的ApplicationContext。

    @RunWith(SpringJUnit4ClassRunner.class)
    @ContextConfiguration(loader = AnnotationConfigContextLoader.class, classes = { MyTestConfig.class })
    @WebAppConfiguration
    public class MyHandlerInterceptorTest  {
    
        @Autowired
        private RequestMappingHandlerMapping requestMappingHandlerMapping;
    
        @Test
        public void myTest() throws Exception {
            MockHttpServletRequest request = new MockHttpServletRequest("GET",
                "/myrequest");
            HandlerExecutionChain handlerExecutionChain = requestMappingHandlerMapping.getHandler(request);
        }
    }
    

    【讨论】:

    • 谢谢 - 我已经弄清楚如何修复我的测试 - 我正在尝试找出根本原因 - 其他测试在哪里做这个?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-03-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-13
    相关资源
    最近更新 更多