Java 从类路径资源解析XML文档时发生IOException-文件不存在-Spring
我知道,关于SpringXMLConfig文件在执行中未被找到的问题以前已经被问过好几次了,但是其他的答案似乎都不适合我 我正试图用Spring和JUnit在Maven项目中运行一些测试,我一直都会遇到这样的错误: T T S T S 正在运行es.udc.jcastedo.NosaTenda.test.model.productoService.ProductoServiceTest 12-ago-2014 13:37:33 org.springframework.test.context.TestContextManager检索执行侦听器 信息:无法实例化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:启动日期[2014年8月12日星期二13:37:33];上下文层次结构的根 12-ago-2014 13:37:33 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions 信息:从类路径资源[nostatendaspring config.XML]加载XMLbean定义Java 从类路径资源解析XML文档时发生IOException-文件不存在-Spring,java,xml,spring,maven,junit4,Java,Xml,Spring,Maven,Junit4,我知道,关于SpringXMLConfig文件在执行中未被找到的问题以前已经被问过好几次了,但是其他的答案似乎都不适合我 我正试图用Spring和JUnit在Maven项目中运行一些测试,我一直都会遇到这样的错误: T T S T S 正在运行es.udc.jcastedo.NosaTenda.test.model.productoService.ProductoServiceTest 12-ago-2014 13:37:33 org.springframework.test.context.T
测试运行:2,失败:0,错误:2,跳过:0,经过的时间:0.225秒通常,您的类路径是…/src/main/java/,ClassPathXmlApplicationContext将从…/src/main/resources读取。 然后您的测试类将位于…/src/test/java/中,ClassPathXmlApplicationContext将从…/src/test/resources中读取
因此,检查测试应用程序上下文的位置,并将xml的副本放在那里。好的,我找到了解决方案,它在其他地方 在运行单个测试类时,我使用了带有forkMode none参数的运行配置forkMode显然已被弃用,并导致了问题。我将其替换为forkCount 0,工作得很好,它发现SpringXML配置文件,甚至测试版本覆盖了一些参数,非常完美。现在,我只需调试配置中存在的大量其他错误:)
谢谢。实际上,我有一个spring配置文件的测试版本,旨在覆盖机制中的一些属性,以便使用不同的数据库进行测试,但现在这并不重要。我试过使用src/test/resources中的另一个xml文件,从“/NosaTenda/src/test/resources/NosaTenda-spring-config-test.xml”到“NosaTenda-spring-config-test.xml”,结果相同。所以我不知道,也许错误发生在完全不同的地方?我如何知道在哪里查找xml文件?谢谢你的快速回答。
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 {
...
}
}
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]]