【问题标题】:Failed to load ApplicationContext from Unit Test: FileNotFound无法从单元测试加载 ApplicationContext:FileNotFound
【发布时间】:2014-09-06 17:43:40
【问题描述】:

我正在创建一个 Maven Spring 项目,其中包括 MVC、数据和安全性。 我的 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 运行时,我得到 ​​p>

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

【问题讨论】:

  • 尝试使用"classpath:/spring/applicationContext.xml"
  • 还是同样的错误
  • 如果使用 Eclipse/STS,请右键单击您的项目 -> 属性 -> Java 构建路径 -> 源选项卡。你有 [project_name]/src/main/resources 作为 Source 文件夹吗?
  • 我认为你可以在没有类路径的情况下使用它:@ContextConfiguration(locations = { "/spring/applicationContext.xml", "/spring/applicationContext-jpa.xml", "/spring/applicationContext-security .xml" })
  • 是的,还有 src/main/java 和 src/test/java

标签: spring junit applicationcontext


【解决方案1】:

尝试使用 * 的相对路径

  @ContextConfiguration(locations = {
"classpath*:spring/applicationContext.xml",
"classpath*:spring/applicationContext-jpa.xml",
"classpath*:spring/applicationContext-security.xml" })

如果不看看你的 xml 是否真的在 resources/spring/.

终于在没有位置的情况下试穿

 @ContextConfiguration({"classpath*:spring/applicationContext.xml"})

您显示的另一个错误是因为您在 applicationContext.xml 和 applicationContext-security.xml 上复制了此标记

 Duplicate <global-method-security>

【讨论】:

  • 您的解决方案只有在将 spring 添加到构建路径后才能工作
  • @ContextConfiguration(locations = { "classpath*:resources/invoices-context.xml","classpath*:resources/invoices-int-schema.xml" }) 这对我有用.. ..谢谢
【解决方案2】:

我将 spring 文件夹添加到构建路径中,并且在 clean&build 之后,它工作了。

【讨论】:

  • 如何将Spring文件夹添加到构建路径?
  • 嗨@abhijitcaps 您可以通过右键单击项目并选择构建路径来将spring文件夹添加到构建路径->配置构建路径..选择源文件夹并将路径添加到spring构建路径...
【解决方案3】:

尝试如下

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

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

【讨论】:

    【解决方案4】:

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

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

    【讨论】:

      【解决方案5】:

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

      1. 文件-> 使缓存/重启无效
      2. 清理并构建项目

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

      【讨论】:

        【解决方案6】:

        给下面的

        @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>
        

        【讨论】:

          【解决方案7】:

          在 pom.xml 中添加如下插件:

          <plugin>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.10</version>
            <configuration>
              <useFile>false</useFile>
            </configuration>
          </plugin>
          

          【讨论】:

            【解决方案8】:

            我遇到了同样的错误并意识到 pom.xml 有 java 1.7 而 STS 编译器指向 Java 1.8。将编译器更改为 1.7 并重新构建后解决了该问题。

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

            【讨论】:

              【解决方案9】:

              检查您的运行调试配置。这个值必须是你的主 Spring Boot 应用类

              如果不同的类你可以 YourMainAplication.java --> 右键 --> 运行

              就是这样!

              【讨论】:

                【解决方案10】:

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

                尝试设置虚拟机选项:

                -da -Xmx2048m -Xms1024m -XX:MaxPermSize=2048m
                

                【讨论】:

                  猜你喜欢
                  • 1970-01-01
                  • 2011-10-18
                  • 2015-03-16
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  • 2018-07-09
                  • 2014-02-28
                  • 2019-03-13
                  相关资源
                  最近更新 更多