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

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. 关于Windows下的访问控制模型

    在探索Windows操作系统的过程中,发现很多有意思 的东西. Windows下的访问控制模型也是我在Github上浏览代码时,无意中发现的. 项目地址 https://github.com/Krut ...

  2. 解决Vs控制台程序出现NuGet\profile.ps1,因为在此系统上禁止运行脚本错误时或提示:“无法加载文件 \.nuget\packages\Microsoft.EntityFrameworkCore.Tools\1.1.0-preview4-final\tools\init.ps1,因为在此系统上禁止运行脚本”

    1,打开Windows PowerShell 2.输入 set-executionpolicy remotesigned 即可执行脚本 如果执行命令失败的话重新选择Windows PowerShell ...

  3. cmake用法及常用命令总结(全)

    CMakeLists.txt 的语法比较简单,由命令.注释和空格组成,其中命令是不区分大小写的.指令是大小写无关的,参数和变量是大小写相关的.但推荐全部使用大写指令.符号 # 后面的内容被认为是注释. ...

  4. 自定义标签之inclusion_tag

    1.在当前app下创建templatetags文件夹 2.在templatetags文件夹下面创建自定义的mytag.py文件 3.在mytag.py文件中的代码 from django.templa ...

  5. iOS - 毛玻璃特效

    iOS7.0的SDK并没有提供给开发者实现毛玻璃效果的API,所以很多人都是通过一些别人封装的框架来实现 iOS7.0(包括)之前还是有系统的类可以实现毛玻璃效果的, 就是 UIToolbar这个类, ...

  6. css 层叠 比较特殊性

    css 层叠: 多个相同的css声明(属性),应用到同一个元素上.当一个标签声明冲突时,浏览器会自动出发层叠机制 1:比较优先级 2:比较特殊性 3:比较源次序 依次经过上面的1,2,3的比较后,最终 ...

  7. 第十八篇:简易版web服务器开发

    在上篇有实现了一个静态的web服务器,可以接收web浏览器的请求,随后对请求消息进行解析,获取客户想要文件的文件名,随后根据文件名返回响应消息:那么这篇我们对该web服务器进行改善,通过多任务.非阻塞 ...

  8. C++——同名隐藏 和 赋值兼容规则

    同名隐藏 一旦子类定义了与父类同名的方法,则父类里面该名字的所有方法都被隐藏了.必须显示指定是父类的方法才可以 #include<iostream> using namespace std ...

  9. 创建第一kubernetes应用以及基本操作(六)

    1.创建一个测试用的deployment kubectl run net-test --image=alpine --replicas= sleep 2.查看获取IP情况 [root@linux ~] ...

  10. 遍历SQL SERVER中所有存储过程和触发器

    如果需要查找某个存储过程或触发器中是否含有某段文本(比如:你想知道有哪些存储过程操作了某个表) 可以这么写 select name from sysobjects o, syscomments s w ...