spring 无法从单元测试加载 ApplicationContext:FileNotFound
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24776669/
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
Failed to load ApplicationContext from Unit Test: FileNotFound
提问by Manu
I am creating a Maven Spring project, which includes MVC, Data and Security. My Spring applicationContext-*.xml files are located at \src\main\resources\spring\
我正在创建一个 Maven Spring 项目,其中包括 MVC、数据和安全性。我的 Spring applicationContext-*.xml 文件位于 \src\main\resources\spring\
My TestCase is placed at \src\test\java\my\package\controller\ and its code is:
我的 TestCase 放在 \src\test\java\my\package\controller\ 并且它的代码是:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {
"classpath:spring/applicationContext.xml",
"classpath:spring/applicationContext-jpa.xml",
"classpath:spring/applicationContext-security.xml" })
public class MyControllerTest extends TestCase {
@Autowired
private MyController myController;
@Test
public void myMethod_test() {
}
}
When I right click on the test class and run as JUnit, I get
当我右键单击测试类并作为 JUnit 运行时,我得到
java.lang.IllegalStateException: Failed to load ApplicationContext
at org.springframework.test.context.CacheAwareContextLoaderDelegate.loadContext(CacheAwareContextLoaderDelegate.java:99)
at org.springframework.test.context.DefaultTestContext.getApplicationContext(DefaultTestContext.java:101)
at
[...]
Caused by: org.springframework.beans.factory.BeanDefinitionStoreException:
IOException parsing XML document from class path resource
[applicationContext.xml]; nested exception is java.io.FileNotFoundException:
class path resource [applicationContext.xml] cannot be opened because it does not exist
[...]
If I try to remove applicationContext.xml from the locations, I still get exactly the same error. The complete stack trace is this:
如果我尝试从这些位置删除 applicationContext.xml,我仍然会遇到完全相同的错误。完整的堆栈跟踪是这样的:
java.lang.IllegalStateException: Failed to load ApplicationContext
at org.springframework.test.context.CacheAwareContextLoaderDelegate.loadContext(CacheAwareContextLoaderDelegate.java:99)
at org.springframework.test.context.DefaultTestContext.getApplicationContext(DefaultTestContext.java:101)
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:331)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTest(SpringJUnit4ClassRunner.java:213)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runReflectiveCall(SpringJUnit4ClassRunner.java:290)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.methodBlock(SpringJUnit4ClassRunner.java:292)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:233)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:87)
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 @ContextConfiguration(locations = {
"classpath*:spring/applicationContext.xml",
"classpath*:spring/applicationContext-jpa.xml",
"classpath*:spring/applicationContext-security.xml" })
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:176)
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 [applicationContext.xml]; nested exception is java.io.FileNotFoundException: class path resource [applicationContext.xml] cannot be opened because it does not exist
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:343)
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:303)
at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:180)
at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:216)
at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:187)
at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:251)
at org.springframework.test.context.support.AbstractGenericContextLoader.loadBeanDefinitions(AbstractGenericContextLoader.java:253)
at org.springframework.test.context.support.AbstractGenericContextLoader.loadContext(AbstractGenericContextLoader.java:122)
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:250)
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 [applicationContext.xml] cannot be opened because it does not exist
at org.springframework.core.io.ClassPathResource.getInputStream(ClassPathResource.java:172)
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:329)
... 37 more
采纳答案by Manu
I added the spring folder to the build path and, after clean&build, it worked.
我将 spring 文件夹添加到构建路径中,并且在 clean&build 之后,它起作用了。
回答by paul
Try with the relative path using *
尝试使用 * 的相对路径
@ContextConfiguration({"classpath*:spring/applicationContext.xml"})
If not look if your xml are really on resources/spring/.
如果不看看你的 xml 是否真的在 resources/spring/ 上。
Finally try just on without location
最后在没有位置的情况下试穿
Duplicate <global-method-security>
The other error that you′re showing is because you have this tag duplicated on applicationContext.xml and applicationContext-security.xml
您显示的另一个错误是因为您在 applicationContext.xml 和 applicationContext-security.xml 上复制了此标记
@ContextConfiguration (locations = "classpath*:/spring/applicationContext*.xml")
回答by Naveen Kumar
try as below
尝试如下
@ActiveProfiles("sandbox")
class MyTestClass...
this will load all 3 of your application context xml file.
这将加载所有 3 个应用程序上下文 xml 文件。
回答by MCMatan
For me, I was missing @ActiveProfileat my test class
对我来说,我在测试课上缺少@ActiveProfile
@ContextConfiguration(locations = {"classpath*:/spring/test-context.xml"})
回答by Akanksha gore
If you are using intellij, then try restarting intellij cache
如果您使用的是 Intellij,请尝试重新启动 Intellij 缓存
- File-> Invalidate cache/restart
- clean and build project
- 文件-> 使缓存无效/重启
- 清理并构建项目
See if it works, it worked for me.
看看它是否有效,它对我有用。
回答by Seba anna
Give the below
给出以下
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.20.1</version>
<configuration>
<additionalClasspathElements>
<additionalClasspathElement>${basedir}/src/test/resources</additionalClasspathElement>
</additionalClasspathElements>
</configuration>
And in pom.xmlgive the following plugin:
并pom.xml提供以下插件:
-da -Xmx2048m -Xms1024m -XX:MaxPermSize=2048m
回答by Ankit Patel
I faced the same error and realized that pom.xml had java 1.7 and STS compiler pointed to Java 1.8. Upon changing compiler to 1.7 and rebuild fixed the issue.
我遇到了同样的错误并意识到 pom.xml 有 java 1.7 并且 STS 编译器指向 Java 1.8。将编译器更改为 1.7 并重建后修复了该问题。
PS: This answer is not related to actual question posted but applies to similar error for app Context not loading
PS:此答案与发布的实际问题无关,但适用于应用上下文未加载的类似错误
回答by codebee
The problem is insufficient memory to load context.
问题是内存不足,无法加载上下文。
Try to set VM options:
尝试设置 VM 选项:
##代码##
