IDEA Junit FileNotFoundException: class path resource [spring/spring.xml] cannot be opened because it does not exist
阿新 • • 發佈:2019-04-10
tst clas 指定 -- init project eclipse 分享 圖片
今天打算寫一個單元測試,但是已經有寫好的單元測試無論怎麽弄都提示文件不存在,自己一度以為是啟動方式不正確.這裏簡單記錄一下處理過程
1 異常信息:
Caused by: org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from class path resource [spring/spring.xml]; nested exception is java.io.FileNotFoundException: class path resource [spring/spring.xml] cannot be opened because it does not exist at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:344) at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:304) at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:181) at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:217) at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:188) at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:252) 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:109) at org.springframework.test.context.support.AbstractDelegatingSmartContextLoader.loadContext(AbstractDelegatingSmartContextLoader.java:261) at org.springframework.test.context.DefaultCacheAwareContextLoaderDelegate.loadContextInternal(DefaultCacheAwareContextLoaderDelegate.java:68) at org.springframework.test.context.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:86) ... 24 more Caused by: java.io.FileNotFoundException: class path resource [spring/spring.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:330) ... 36 more
2 項目目錄結構
3 處理方式
1. 首先是獲取單元測試工作目錄
@Test public void demoTest(){ System.out.println(AccountTest.class.getClassLoader().getResource("").getPath()); }
輸出信息:
/D:/git/zzlh/guarantee/target/test-classes/
2. 然後去這個目錄下查找是否有自己的配置文件 -> spring/spring.xml
發現確實沒有,這時我想到是不是IDEA的原因,之前用eclipse也常是這種因為build path的原因造成文件找不到
3 IDEA需要你指定單元測試的resouces目錄
a. 如果你test目錄下有resouces目錄文件,需要如下操作
確認 test/resources 目錄為 [test/resources] --右鍵--> [Mark Directory as] -->[Test Resources Root]
b. 如果沒有需要添加test的resource目錄
File -> Project Structrue... -> Project Settings -> Modules 進行如下配置
IDEA Junit FileNotFoundException: class path resource [spring/spring.xml] cannot be opened because it does not exist