java Spring MVC 和 JUnit:无法加载 ApplicationContext
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17628528/
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
Spring MVC and JUnit: Failed to load ApplicationContext
提问by user1883212
In order to test my DAO and automatically autowire my objects, I created the following test class in JUnit:
为了测试我的 DAO 并自动装配我的对象,我在 JUnit 中创建了以下测试类:
@RunWith( SpringJUnit4ClassRunner.class )
@ContextConfiguration(locations={"/spring-servlet.xml"})
public class MyTest {
// Other stuff here
}
After running it, I get the following error:
运行后,我收到以下错误:
java.lang.IllegalStateException: Failed to load ApplicationContext
at org.springframework.test.context.CacheAwareContextLoaderDelegate.loadContext(CacheAwareContextLoaderDelegate.java:99)
at org.springframework.test.context.TestContext.getApplicationContext(TestContext.java:122)
at org.springframework.test.context.web.ServletTestExecutionListener.setUpRequestContextIfNecessary(ServletTestExecutionListener.java:105)
at org.springframework.test.context.web.ServletTestExecutionListener.prepareTestInstance(ServletTestExecutionListener.java:74)
at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:312)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTest(SpringJUnit4ClassRunner.java:211)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runReflectiveCall(SpringJUnit4ClassRunner.java:288)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.methodBlock(SpringJUnit4ClassRunner.java:284)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:231)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:88)
at org.junit.runners.ParentRunner.run(ParentRunner.java:238)
at org.junit.runners.ParentRunner.schedule(ParentRunner.java:63)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
at org.junit.runners.ParentRunner.access MyProject\src\main\java\org\baudo\dao
0(ParentRunner.java:53)
at org.junit.runners.ParentRunner.evaluate(ParentRunner.java:229)
at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:71)
at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:174)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
Caused by: org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from class path resource [spring-servlet.xml]; nested exception is java.io.FileNotFoundException: class path resource [spring-servlet.xml] cannot be opened because it does not exist
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:341)
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:302)
at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:174)
at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:209)
at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:180)
at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:243)
at org.springframework.test.context.support.AbstractGenericContextLoader.loadBeanDefinitions(AbstractGenericContextLoader.java:233)
at org.springframework.test.context.support.AbstractGenericContextLoader.loadContext(AbstractGenericContextLoader.java:117)
at org.springframework.test.context.support.AbstractGenericContextLoader.loadContext(AbstractGenericContextLoader.java:60)
at org.springframework.test.context.support.AbstractDelegatingSmartContextLoader.delegateLoading(AbstractDelegatingSmartContextLoader.java:100)
at org.springframework.test.context.support.AbstractDelegatingSmartContextLoader.loadContext(AbstractDelegatingSmartContextLoader.java:248)
at org.springframework.test.context.CacheAwareContextLoaderDelegate.loadContextInternal(CacheAwareContextLoaderDelegate.java:64)
at org.springframework.test.context.CacheAwareContextLoaderDelegate.loadContext(CacheAwareContextLoaderDelegate.java:91)
... 25 more
Caused by: java.io.FileNotFoundException: class path resource [spring-servlet.xml] cannot be opened because it does not exist
at org.springframework.core.io.ClassPathResource.getInputStream(ClassPathResource.java:157)
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:328)
... 37 more
My Dao is under:
我的道在:
MyProject\src\test\java\org\baudo\dao
My test is under:
我的测试是在:
MyProject\src\main\webapp\WEB-INF
My spring-servlet.xml is under:
我的 spring-servlet.xml 位于:
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="configLocation">
<value>classpath:hibernate.cfg.xml</value>
</property>
<property name="configurationClass">
<value>org.hibernate.cfg.AnnotationConfiguration</value>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">${jdbc.dialect}</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
</bean>
I tried to pass to @ContextConfiguration many different paths and I placed my spring-servlet.xml in every possible path of the application. This cause me to think it's not a path problem and I think there is something else going on.
我尝试将许多不同的路径传递给 @ContextConfiguration,并将 spring-servlet.xml 放置在应用程序的每个可能路径中。这让我认为这不是路径问题,我认为还有其他事情发生。
This is how I configured the object I want to autowire in Spring:
这就是我配置要在 Spring 中自动装配的对象的方式:
<tx:annotation-driven />
It's correctly autowired when I run the application, but not during the tests. That's the reason I added @ContextConfiguration.
它在我运行应用程序时正确自动装配,但在测试期间不是。这就是我添加@ContextConfiguration 的原因。
I also have that line in my spring configuration:
我的弹簧配置中也有该行:
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>3.2.3.RELEASE</version>
</dependency>
This are de dependencies I added for tests into the pom.xml
这是我为测试添加到 pom.xml 中的依赖项
##代码##Oher info: this is a web app with Spring MVC and Hibernate. I run unit tests by right-clicking the test file and selecting "run as"->"JUnit test"
其他信息:这是一个带有 Spring MVC 和 Hibernate 的 Web 应用程序。我通过右键单击测试文件并选择“运行方式”->“JUnit 测试”来运行单元测试
But I noticed Eclipse doesn't care of this particular JUnit, because it uses its own from the Java Development Tools plugin. However it's JUnit 4.
但我注意到 Eclipse 并不关心这个特定的 JUnit,因为它使用来自 Java Development Tools 插件的它自己的。然而,它是 JUnit 4。
What's wrong? How can I enable autowiring for unit testings too?
怎么了?如何为单元测试启用自动装配?
回答by JB Nizet
As the stack trace indicates, the Spring runner looks for the XML file in the claspath. So assuming a standard Maven layout, and since this XML file is used by tests, it should be under src/test/resources
.
正如堆栈跟踪所示,Spring runner 在 claspath 中查找 XML 文件。所以假设一个标准的 Maven 布局,并且由于这个 XML 文件被测试使用,它应该在src/test/resources
.
It seems like you want to use the same file for your application and your tests. This is a strange choice, since DAO tests shouldn't use the same beans (controllers, etc.) as the real MVC application.
您似乎想为您的应用程序和测试使用相同的文件。这是一个奇怪的选择,因为 DAO 测试不应该使用与真正的 MVC 应用程序相同的 bean(控制器等)。