junit测试时找不到资源路径java.io.FileNotFoundException: class path resource [config/EBSConfig.xml] cannot be o

时间:2022-12-26 00:32:29

今天测试时需要使用 WEB-INF 下的见容,但报异常:(我们的项目默认的类路径是 src/main/resources)

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)
 at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
 at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.methodBlock(SpringJUnit4ClassRunner.java:290)
 at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:231)
 at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:47)
 at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231)
 at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60)
 at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)
 at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50)
 at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222)
 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:300)
 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 [config/EBSConfig.xml]; nested exception is java.io.FileNotFoundException: class path resource [config/EBSConfig.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:205)
 at org.springframework.test.context.support.AbstractGenericContextLoader.loadContext(AbstractGenericContextLoader.java:100)
 at org.springframework.test.context.support.AbstractGenericContextLoader.loadContext(AbstractGenericContextLoader.java:1)
 at org.springframework.test.context.support.DelegatingSmartContextLoader.loadContext(DelegatingSmartContextLoader.java:228)
 at org.springframework.test.context.TestContext.loadApplicationContext(TestContext.java:124)
 at org.springframework.test.context.TestContext.getApplicationContext(TestContext.java:148)
 ... 24 more
Caused by: java.io.FileNotFoundException: class path resource [config/EBSConfig.xml] cannot be opened because it does not exist
 at org.springframework.core.io.ClassPathResource.getInputStream(ClassPathResource.java:158)
 at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:328)
 ... 35 more

 

解决办法:把要使用的文件复制到已存在的 classpath 目录下;
 
或者:
打开web项目下的.classpath文件,其内容如下:
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
 <classpathentry kind="src" output="target/classes" path="src/main/java">
  <attributes>
   <attribute name="optional" value="true"/>
   <attribute name="maven.pomderived" value="true"/>
  </attributes>
 </classpathentry>
 <classpathentry excluding="**" kind="src" output="target/classes" path="src/main/resources">
  <attributes>
   <attribute name="maven.pomderived" value="true"/>
  </attributes>
 </classpathentry>
 <classpathentry kind="src" output="target/test-classes" path="src/test/java">
  <attributes>
   <attribute name="optional" value="true"/>
   <attribute name="maven.pomderived" value="true"/>
  </attributes>
 </classpathentry>
 <classpathentry excluding="**" kind="src" output="target/test-classes" path="src/test/resources">
  <attributes>
   <attribute name="maven.pomderived" value="true"/>
  </attributes>
 </classpathentry>
 <classpathentry kind="src" path="/basic"/>
 <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.6">
  <attributes>
   <attribute name="maven.pomderived" value="true"/>
  </attributes>
 </classpathentry>
 <classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
  <attributes>
   <attribute name="maven.pomderived" value="true"/>
  </attributes>
 </classpathentry>
 <classpathentry kind="output" path="target/classes"/>
</classpath>
这里并没有看到类路径包含web-inf目录。
 
因此可以往里边添加<classpathentry kind="src" path="你需要使用的路径"/>