Junit with Spring-TestContextManager[ERROR]在允许TestExecutionListener时捕获到异常
在我的Spring Maven——Tomcat web应用程序上的Hibernate Mysql runnint中,我使用两种不同的Junit类别运行了两种类型的Junit集成测试:Junit with Spring-TestContextManager[ERROR]在允许TestExecutionListener时捕获到异常,spring,tomcat,maven,junit,integration-testing,Spring,Tomcat,Maven,Junit,Integration Testing,在我的Spring Maven——Tomcat web应用程序上的Hibernate Mysql runnint中,我使用两种不同的Junit类别运行了两种类型的Junit集成测试: LocalTests—在进程中运行(不需要服务器),直接调用我的web层方法(本例中为jersey) HttpTests——模拟客户端并通过http请求调用我的web层,需要启动并运行tomcat 在每个测试等级之上,我有: @RunWith(SpringJUnit4ClassRunner.class) @Cont
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath:testApplicationContext.xml" })
我的测试套件是这样的(这是2个测试套件中的一个,我为每个类别都提供了一个测试套件):
我的testApplicationContext实际上是空的,它只包含组件扫描:
<context:component-scan base-package="com.company.app" />
当我运行本地测试时,一切都很顺利,但当我调用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$1.runReflectiveCall(SpringJUnit4ClassRunner.java:288)
2012-07-22 17:56:13 DefaultListableBeanFactory[INFO]在org.springframework.beans.factory.support中销毁单例。DefaultListableBeanFactory@2598a35d:定义bean[httpBrandManagerTestsProxy,httpClubTestsProxy,问题发生的原因是,Spring上下文在我的一个侦听器之前加载,该侦听器在web.xml
中定义,Spring正在初始化一些使用非Spring类的bean,这些bean使用我自己的侦听器初始化。为了解决这个问题,我应该re my listener首先运行。对于我来说,添加虚拟机选项起作用了
将此添加到该测试用例的VM选项中:-Djavax.xml.parsers.DocumentBuilderFactory=com.sun.org.apache.xerces.internal.jaxp.documentbuilderfactorympl-Djavax.xml.parsers.SAXParserFactory=com.sun.org.apache.xerces.internal.jaxp.saxparserfactorympl,除了上面提到的答案之外,您还应该查找“引起的”查看是否存在任何依赖项解析问题。例如,如果您的测试类为MyService.java,并且它使用了依赖项MyRepository.java。那么您应该确保所有这些依赖项都在测试类中被模拟或自动连接。堆栈跟踪是否缺少由“s”引起的几个“s”?以下所有“由s引起的”说明:由:org.springframework.beans.factory.BeanCreationException和我的spring beans引起。据我所知,它的信息量不大。我是否遗漏了一些内容?如果不看它,我可以说由
引起的最后一条消息准确地描述了问题所在。你能发布完整的堆栈跟踪吗(你可以编辑你的问题)?Tomasz-你是对的,我不得不深入研究堆栈跟踪,但最终解决方案出现了,在一段很长的“由引起的”结尾布洛克,谢谢!你是如何让听众跑步的first@muthu这一次已经过去了一段时间,我很抱歉地说,我真的不记得我以前做过什么。此外,我对(我的)这个简洁的回答感到非常不安,非常抱歉:(@forhas我刚才遇到了同样的问题,你的回答启发了我。我提取了测试类的超类,它扩展了AbstractJUnit4S普林gContextTests,但RunWith和ContextConfiguration注释仅在派生类上进行注释。添加这些注释后,测试通过。-@forhas Laconic answer将简短但解释性:)
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$1.runReflectiveCall(SpringJUnit4ClassRunner.java:288)