Spring 未能从单元测试加载ApplicationContext:FileNotFound

Spring 未能从单元测试加载ApplicationContext:FileNotFound,spring,junit,applicationcontext,Spring,Junit,Applicationcontext,我正在创建一个Maven Spring项目,其中包括MVC、数据和安全性。 My Spring applicationContext-*.xml文件位于\src\main\resources\Spring\ 我的TestCase位于\src\test\java\My\package\controller\处,其代码为: @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = { "classpat

我正在创建一个Maven Spring项目,其中包括MVC、数据和安全性。 My Spring applicationContext-*.xml文件位于\src\main\resources\Spring\

我的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() {
    }
}
当我右键单击测试类并作为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 
[...]
如果我试图从这些位置删除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$1.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$3.run(ParentRunner.java:238)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
at org.junit.runners.ParentRunner$2.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

尝试使用相对路径*

  @ContextConfiguration(locations = {
"classpath*:spring/applicationContext.xml",
"classpath*:spring/applicationContext-jpa.xml",
"classpath*:spring/applicationContext-security.xml" })
如果没有,请查看您的xml是否真的在参考资料/spring/中

最后在没有位置的情况下试穿

 @ContextConfiguration({"classpath*:spring/applicationContext.xml"})
您显示的另一个错误是,您在applicationContext.xml和applicationContext-security.xml上复制了此标记

 Duplicate <global-method-security>
重复
尝试以下方法

@ContextConfiguration (locations = "classpath*:/spring/applicationContext*.xml")

这将加载所有3个应用程序上下文xml文件。

我将spring文件夹添加到生成路径中,在清理和生成之后,它工作正常。

问题是内存不足,无法加载上下文

尝试设置VM选项:

-da -Xmx2048m -Xms1024m -XX:MaxPermSize=2048m
给出下面的答案

@ContextConfiguration(locations =  {"classpath*:/spring/test-context.xml"})
pom.xml
中提供以下插件:

<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>
<plugin>
  <artifactId>maven-surefire-plugin</artifactId>
  <version>2.10</version>
  <configuration>
    <useFile>false</useFile>
  </configuration>
</plugin>

org.apache.maven.plugins
maven surefire插件
2.20.1
${basedir}/src/test/resources

我遇到了同样的错误,并意识到pom.xml有java 1.7,STS编译器指向java 1.8。将编译器更改为1.7并重新生成后,修复了该问题


PS:此答案与实际发布的问题无关,但适用于应用程序上下文未加载的类似错误

对于我来说,我在测试课上缺少了@ActiveProfile

@ActiveProfiles("sandbox")
class MyTestClass...

如果您正在使用intellij,请尝试重新启动intellij缓存

  • 文件->使缓存无效/重新启动
  • 清洁和建设项目

  • 看看它是否有效,它对我有效。

    pom.xml中的外接程序提供以下插件:

    <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>
    
    <plugin>
      <artifactId>maven-surefire-plugin</artifactId>
      <version>2.10</version>
      <configuration>
        <useFile>false</useFile>
      </configuration>
    </plugin>
    
    
    maven surefire插件
    2.10
    假的
    
    尝试使用
    “classpath:/spring/applicationContext.xml”
    仍然存在相同的错误如果使用Eclipse/STS,右键单击项目->属性->Java构建路径->源选项卡。您是否有[project_name]/src/main/resources作为源文件夹?我认为您可以在没有类路径的情况下使用它:@ContextConfiguration(locations={/spring/applicationContext.xml“,“/spring/applicationContext jpa.xml”,“/spring/applicationContext security.xml})是,与src/main/java和src/test/java一起,您的解决方案只有在将spring添加到构建中之后才能工作path@ContextConfiguration(位置={“classpath*:resources/invoices context.xml”,“classpath*:resources/invoices int schema.xml”})这对我来说很有用…ThanksHow将Spring文件夹添加到生成路径?Hi@abhijitcaps您可以通过右键单击project并选择build path->configure build path将Spring文件夹添加到生成路径。。选择源文件夹并将路径添加到spring生成路径。。。