zoukankan      html  css  js  c++  java
  • IDEA Junit FileNotFoundException: class path resource [spring/spring.xml] cannot be opened because it does not exist

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

    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 进行如下配置

            

  • 相关阅读:
    Django项目总结: REST Framework 用户注册登录,权限控制,级联操作查询,节流控制自定义
    画皮卡丘
    软件测试 (实际输出与预期输出间的比较过程)
    软件测试方法
    Vue模板语法
    HTTP 与 HTTPS 的区别
    tomcat运行多个项目同一个端口与不同端口的设置
    springboot-WebMvcConfigurer配置拦截器/跨域/格式化/注册控制器
    Spring IOC(控制反转)思想笔记
    工程师最重要的能力是什么?
  • 原文地址:https://www.cnblogs.com/chengmuyu/p/10685522.html
Copyright © 2011-2022 走看看