今天打算写一个单元测试,但是已经有写好的单元测试无论怎么弄都提示文件不存在,自己一度以为是启动方式不正确.这里简单记录一下处理过程

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的更多相关文章

  1. parsing XML document from class path resource [config/applicationContext.xml]; nested exception is java.io.FileNotFoundException: class path resource [config/applicationContext.xml] 解决方案

    parsing XML document from class path resource [config/applicationContext.xml]; nested exception is j ...

  2. idea的maven项目运行出错_java.io.FileNotFoundException: class path resource [spring/sprint-tx.xml] cannot be opened because it does not exist

    前提:idea  maven  ssm 错误信息如下: 严重: Exception sending context initialized event to listener instance of ...

  3. spring整合mybatis错误:class path resource [config/spring/springmvc.xml] cannot be opened because it does not exist

    spring 整合Mybatis 运行环境:jdk1.7.0_17+tomcat 7 + spring:3.2.0 +mybatis:3.2.7+ eclipse 错误:class path reso ...

  4. class path resource [spring/ApplicationContext-springmvc.xml] cannot be opened because it does not exist

    配置如下: <init-param>   <param-name>contextConfigLocation</param-name>   <param-va ...

  5. class path resource [spring/applicationContext.xml] cannot be opened because it does not exist

    1.查看路径有没有写错 2.编辑器认为你的文件不是 source folders(原文件),需要你手动将文件改过来

  6. nested exception is java.io.FileNotFoundException: class path resource [spring/spring-datasource-mog

    spring单元測试时发现的问题: org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsin ...

  7. Caused by: java.io.FileNotFoundException: class path resource [spring/springmvc.xml] cannot be opene

                        Caused by: java.io.FileNotFoundException: class path resource [spring/springmvc. ...

  8. class path resource [processes/] cannot be resolved to URL because it does not exist

    springboot整合activiti时报以下错误,原因是项目启动时检查流程文件 nested exception is java.io.FileNotFoundException: class p ...

  9. Spring报错:java.io.FileNotFoundException: class path resource [applicationContext.xml] cannot be opened because it does not exist

    感谢:http://blog.chinaunix.net/uid-20681545-id-184633.html提供的解决方案,非常棒 ! 问题说明: 新建一个Spring项目,新建一个Bean类:H ...

随机推荐

  1. WCF header 域

    [OperationContract] [WebInvoke(UriTemplate = "poststr1")] public string poststr1(csinfo cs ...

  2. SSH和SSM对比异同点、各自优势

    1SSH和SSM定义SSH 通常指的是 Struts2 做控制器(Action),Spring 管理各层的组件,Hibernate 负责持久化层. SSM 则指的是 SpringMVC 做控制器(co ...

  3. Http 和 Socket 之间的恩爱情仇

    前言 一些刚入门的小伙伴可能会用 Socket,也会用 OkHttp 或者 HttpUrlConnection 等一些 HTTP 客户端工具,这两个东西看着有点像可是又不太一样,到底是哪里不一样呢? ...

  4. J.U.C之Condition

    此篇博客所有源码均来自JDK 1.8 在没有Lock之前,我们使用synchronized来控制同步,配合Object的wait().notify()系列方法可以实现等待/通知模式.在Java SE5 ...

  5. 关于Vue-ElementUI修改默认样式不成功问题解决

    Element是一个很好用的组件库,但是有时候我们需要修改一些组件的样式以满足我们自己的需求. 我们用浏览器调试找到相应的class,在本地重写这个class时,发现修改不成功. 这是因为在Vue文件 ...

  6. 微信开发者工具 关于no such file or directory

    在新建页面中,保存后弹出 “ no such file or directory ” 错误 原因是打开了一个文件,然后在目录树中删除了它,但是这个被删除的页面依旧在打开状态,开发者工具在编译保存时由于 ...

  7. iOS 中 UIView 和 CALayer 的关系

    UIView 有一个名叫 layer ,类型为 CALayer 的对象属性,它们的行为很相似,主要区别在于:CALayer 继承自 NSObject ,不能够响应事件. 这是因为 UIView 除了负 ...

  8. sed 删除文本中的内容

    删除命令对照表 练习例子 删除/etc/passwd中的第15行 sed -i '1d' passwd 删除/etc/passwd中的8行到14行的所有内容 sed -i '8,14d' passwd ...

  9. django操作mysql

    连接mysql 1.安装pymysql 操作指令 : pymsql: pip install pymysql 2.导入库 在项目目录下的__init__.py文件中导入pymysql模块 加入以下两行 ...

  10. php 图片与base64互转

    header('Content-type:text/html;charset=utf-8'); //读取图片文件,转换成base64编码格式 $image_file = '1.png'; $image ...