java - 如何识别其他 JUnit 测试何时窃取我的 Spring Root Controller ?

标签 java spring unit-testing spring-mvc junit

我有一个看起来像这样的 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 Root Controller ?

最佳答案

尝试将 @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);
    }
}

关于java - 如何识别其他 JUnit 测试何时窃取我的 Spring Root Controller ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36128823/

相关文章:

java - GridBagLayout,如何在放置组件时阻止屏幕拉伸(stretch)?

java - 我应该在多大程度上隔离我的单元测试方法?

spring - 在 @RepositoryRestResource 中导出 PATCH/PUT 但不导出 POST

c++ - 如何使模拟对象在 Google Mock 中引发异常?

javascript - AngularJs - 如何使用私有(private)方法编写可测试的 Controller

Java NIO 服务器

Android 应用程序中的 java.lang.NullPointerException。

java - Quartz CRON 触发器未按预期触发

java - 使用 Spring Integration 使 ssl-context-support 有条件地使用 tcp-connection-factory

java - Mockito:如何模拟类型 `.class`