【问题标题】:IOException parsing XML document from class path resource - file does not exist - SpringIOException从类路径资源解析XML文档-文件不存在-Spring
【发布时间】:2014-08-12 12:12:39
【问题描述】:

我知道这个关于 spring xml config file not found in execution 的问题之前已经被问过好几次了,但其他答案似乎都不适合我。

我正在尝试使用 Spring 和 JUnit 在 Maven 项目中运行一些测试,但我一直收到此错误:

T E S T S 运行 es.udc.jcastedo.NosaTenda.test.model.productoService.ProductoServiceTest 12-ago-2014 13:37:33 org.springframework.test.context.TestContextManager retrieveTestExecutionListeners 信息:无法实例化 TestExecutionListener [org.springframework.test.context.web.ServletTestExecutionListener]。指定自定义侦听器类或使默认侦听器类(及其所需的依赖项)可用。违规类:[javax/servlet/ServletContext] 12-ago-2014 13:37:33 org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh 信息:刷新 org.springframework.context.support.ClassPathXmlApplicationContext@2f8bbc98:启动日期 [Tue Aug 12 13:37:33 CEST 2014];上下文层次的根 12-ago-2014 13:37:33 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions 信息:从类路径资源 [nosaTenda-spring-config.xml] 加载 XML bean 定义 测试运行:2,失败:0,错误:2,跳过:0,经过时间:0.225 秒

结果:

测试错误: es.udc.jcastedo.NosaTenda.test.model.productoService.ProductoServiceTest es.udc.jcastedo.NosaTenda.test.model.productoService.ProductoServiceTest:无法初始化类 es.udc.jcastedo.NosaTenda.test.model.util.DbUtil

测试运行:2,失败:0,错误:2,跳过:0

这是肯定报告完整跟踪的链接:https://dl.dropboxusercontent.com/u/2635926/es.udc.jcastedo.NosaTenda.test.model.productoService.ProductoServiceTest.txt

在哪里可以找到这些行:

原因:org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from class path resource [nosaTenda-spring-config.xml];嵌套异常是 java.io.FileNotFoundException: 类路径资源 [nosaTenda-spring-config.xml] 无法打开,因为它不存在

原因:java.io.FileNotFoundException:类路径资源[nosaTenda-spring-config.xml]无法打开,因为它不存在

es.udc.jcastedo.NosaTenda.test.model.productoService.ProductoServiceTest 已用时间:0.003 秒

DbUtil 是一个被所有测试调用的辅助类,上下文被初始化:

public class DbUtil {
    static {
        ApplicationContext context = new ClassPathXmlApplicationContext("nosaTenda-spring-config.xml");
        transactionManager = (PlatformTransactionManager) context
            .getBean("transactionManager");
        productoDao = (ProductoDao) context.getBean("productoDao");
        tiendaDao = (TiendaDao) context.getBean("tiendaDao");
    }

    ...

    public static void populateDb() {
    ...
    }
    public static void cleanDb() throws Throwable {
    ...
    }
}

显然存在问题,因为似乎无论如何都找不到 xml 配置文件,并且它在 ClassPathXmlApplicationContext 方法调用时中断。

理论上,spring 配置文件应该在 src/main/resources 中。

我尝试了所有组合,从限定名称“/NosaTenda/src/main/resources/nosaTenda-spring-config.xml”到“nosaTenda-spring-config.xml”,结果相同。

我不知道类路径出了什么问题,或者问题是否出在其他地方。

编辑

这是项目的 pom,我只记得我在 build-resources 部分添加了一些过滤,也许我在那里做错了什么,这就是问题所在。

https://dl.dropboxusercontent.com/u/2635926/pom.xml

【问题讨论】:

    标签: java xml spring maven junit4


    【解决方案1】:

    通常你的类路径是 .../src/main/java/,而 ClassPathXmlApplicationContext 将从 .../src/main/resources 中读取。 然后您的测试类将位于 .../src/test/java/ 中,而 ClassPathXmlApplicationContext 将从 .../src/test/resources 中读取。

    因此,请检查您的测试应用程序上下文在哪里,并将 xml 的副本放在那里。

    【讨论】:

    • 其实我有一个测试版的spring配置文件,打算覆盖机制中的一些属性以使用不同的数据库进行测试,不过现在已经不重要了。我已经尝试使用位于 src/test/resources 中的其他 xml 文件,从“/NosaTenda/src/test/resources/nosaTenda-spring-config-test.xml”到“nosaTenda-spring-config-test.xml”,同样的结果。所以我不知道,也许错误出在完全不同的地方?我怎么知道在哪里寻找 xml 文件? PD:感谢您的快速回答。
    【解决方案2】:

    好的,我找到了解决方案,它在其他地方。

    我正在使用带有 forkMode none 参数的运行配置,因为我正在运行单个测试类。 forkMode 显然已被弃用并导致了问题。我用 forkCount 0 替换它并且工作正常,它找到了 spring xml 配置文件,甚至测试版本覆盖了一些参数,非常完美。现在我只需要调试配置中存在的大量其他错误:)

    INFO: Loading XML bean definitions from class path resource [nosaTenda-spring-config.xml]
    12-ago-2014 17:42:09 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
    INFO: Loading XML bean definitions from class path resource [nosaTenda-spring-config-test.xml]
    12-ago-2014 17:42:09 org.springframework.beans.factory.support.DefaultListableBeanFactory registerBeanDefinition
    INFO: Overriding bean definition for bean 'dataSource': replacing [Generic bean: class [org.springframework.jndi.JndiObjectFactoryBean]; scope=; abstract=false; lazyInit=false; autowireMode=0; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=null; factoryMethodName=null; initMethodName=null; destroyMethodName=null; defined in class path resource [nosaTenda-spring-config.xml]] with [Generic bean: class [org.springframework.jdbc.datasource.SingleConnectionDataSource]; scope=; abstract=false; lazyInit=false; autowireMode=0; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=null; factoryMethodName=null; initMethodName=null; destroyMethodName=null; defined in class path resource [nosaTenda-spring-config-test.xml]]
    

    谢谢。

    【讨论】:

      猜你喜欢
      • 2011-08-21
      • 1970-01-01
      • 2016-12-07
      • 1970-01-01
      • 2020-06-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-12-13
      相关资源
      最近更新 更多