Junit with Spring - TestContextManager [ERROR] 在允许 TestExecutionListener 时捕获异常

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11601612/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 05:16:11  来源:igfitidea点击:

Junit with Spring - TestContextManager [ERROR] Caught exception while allowing TestExecutionListener

springtomcatmavenjunitintegration-testing

提问by forhas

In my Spring-Maven--Hibernate-Mysql runnint on Tomcat web app I'm running 2 types of Junit integration tests using 2 different Junit categories:

在 Tomcat Web 应用程序上的 Spring-Maven--Hibernate-Mysql runnint 中,我使用 2 个不同的 Junit 类别运行 2 种类型的 Junit 集成测试:

  1. LocalTests - running in process (no server required), directly invoking my web layer methods(jersey in this case).
  2. HttpTests - Simulating a client and calling my web-layer via an http request, requires tomcat up and running.
  1. LocalTests - 在进程中运行(不需要服务器),直接调用我的 web 层方法(在这种情况下是 jersey)。
  2. HttpTests - 模拟客户端并通过 http 请求调用我的 web 层,需要启动并运行 tomcat。

Above each test class I have:

在每个测试类之上我有:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath:testApplicationContext.xml" })

And my test suite looks like that (this is one of 2, I have one for each category):

我的测试套件看起来像这样(这是 2 个之一,每个类别我都有一个):

@RunWith(Categories.class)
@IncludeCategory(HttpTest.class)
@SuiteClasses({ ...All My Test Classes... })
public class HttpSuiteITCase {

    // Excluded: NotificationTests, ImageHttpTests

    /**
     * Run once before any of the test methods.
     */
    @BeforeClass
    public static void setTestsConfigurations() {
    TestConfiguration.setup(false);
    }

My testApplicationContext is actually empty, it only contains the component scan:

我的 testApplicationContext 实际上是空的,它只包含组件扫描:

<context:component-scan base-package="com.company.app" />

As I run my local tests it everything works smoothly, but as I'm invoking mt HTTP tests it crashes:

当我运行本地测试时,一切正常,但是当我调用 mt HTTP 测试时,它崩溃了:

2012-07-22 17:56:13 DefaultListableBeanFactory [INFO] Destroying singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@2598a35d: defining beans [httpBrandManagerTestsProxy,httpClubTestsProxy,<HERE THERE'S A BUNCH OF SPRING BEANS>,org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,contextApplicationContextProvider,org.springframework.context.annotation.ConfigurationClassPostProcessor$ImportAwareBeanPostProcessor#0]; root of factory hierarchy
2012-07-22 17:56:13 TestContextManager [ERROR] Caught exception while allowing TestExecutionListener [org.springframework.test.context.support.DependencyInjectionTestExecutionListener@29565e9d] to prepare test instance [integrationTests.http.tests.UserLoginDataHttpTests@480d41f3]
java.lang.IllegalStateException: Failed to load ApplicationContext
    at org.springframework.test.context.TestContext.getApplicationContext(TestContext.java:157)
    at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.injectDependencies(DependencyInjectionTestExecutionListener.java:109)
    at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.prepareTestInstance(DependencyInjectionTestExecutionListener.java:75)
    at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:321)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTest(SpringJUnit4ClassRunner.java:211)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runReflectiveCall(SpringJUnit4ClassRunner.java:288)

I tried tons of stuff nothing works :( My testApplicationContext is under the test/resources folder.

我尝试了很多东西,但没有任何效果:(我的 testApplicationContext 在 test/resources 文件夹下。

I have noticed that the exception marks a specific class: UserLoginDataHttpTests. But I can't see anything special about this class, just a regular spring bean.

我注意到异常标记了一个特定的类:UserLoginDataHttpTests。但是我看不出这个类有什么特别之处,只是一个普通的春豆。

Thanks in advance!

提前致谢!

回答by forhas

The problem occurs since Spring context gets loaded before one of my listeners, which is defined in web.xml. Spring is initializing some beens that uses non-spring clases that gets initialized using my own listener. To solve the issue I should make sure my listener runs first.

问题发生是因为 Spring 上下文在我的一个侦听器之前加载,它在 web.xml 中定义。Spring 正在初始化一些使用非 spring 类的 bes,这些类使用我自己的侦听器进行初始化。为了解决这个问题,我应该确保我的听众首先运行。

回答by Ajay

For me adding in VM Options worked.

对我来说,添加 VM 选项有效

Added this to VM Options for that test case: -Djavax.xml.parsers.DocumentBuilderFactory=com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderFactoryImpl -Djavax.xml.parsers.SAXParserFactory=com.sun.org.apache.xerces.internal.jaxp.SAXParserFactoryImpl

将此添加到该测试用例的 VM 选项: -Djavax.xml.parsers.DocumentBuilderFactory=com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderFactoryImpl -Djavax.xml.parsers.SAXParserFactory=com.sun.org.apache .xerces.internal.jaxp.SAXParserFactoryImpl