buc*_*k64 9 java spring unit-testing
我是Spring的新手,并继承了一个Spring项目,该项目在ProjectName/WebContent/WEB-INF/applicationContext.xml中具有所有XML配置.我正在尝试将配置分解为不同的组件,以便在测试时更容易替换DataSources和Hibernate配置等内容.
这是我的文件结构:
ProjectName
->WebContent
->WEB-INF
->applicationContext.xml
->spring-datasource.xml
->spring-hibernate-properties.xml
->spring-persistence.xml
->test
->us.mn.k12... (Java pkgs with JUnit tests)
->spring-hsqldb-datasource.xml
->spring-test-bean-locations.xml
->spring-test-hibernate-properties.xml
->src
->us.mn.k12... (Java pkgs with production code)
Run Code Online (Sandbox Code Playgroud)
在WEB-INF/applicationContext.xml中,我导入以下内容:
<import resource="spring-datasource.xml"/> <!-- Production datasource -->
<import resource="spring-hibernate-properties.xml"/> <!-- Production hibernate properties -->
<import resource="spring-persistence.xml"/> <!-- DAO's, hibernate .hbm.xml mapping files -->
Run Code Online (Sandbox Code Playgroud)
该应用程序使用上述配置.
我的JUnit测试使用DbUnit和HSQLDB内存数据库运行.所以我的JUnit测试引用了spring-test-bean-locations.xml,它具有以下内容:
<import resource="spring-hsqldb-datasource.xml"/> <!-- HSQLDB datasource for test -->
<import resource="../WebContent/WEB-INF/spring-persistence.xml"/> <!-- Production DAO's, hibernate .hbm.xml mapping files -->
<import resource="spring-test-hibernate-properties.xml"/> <!-- Hibernate properties for test -->
Run Code Online (Sandbox Code Playgroud)
通过这种方式,我可以指定测试数据源和hibernate属性,但是重用DAO的生成映射文件等.但是,运行JUnit测试时出错.以下是例外的相关部分:
Caused by: org.springframework.beans.factory.parsing.BeanDefinitionParsingException: Configuration problem: Failed to import bean definitions from relative location [../WebContent/WEB-INF/spring-persistence.xml]
Offending resource: class path resource [spring-test-bean-locations.xml]; nested exception is org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from class path resource [../WebContent/WEB-INF/spring-persistence.xml]; nested exception is java.io.FileNotFoundException: class path resource [../WebContent/WEB-INF/spring-persistence.xml] cannot be opened because it does not exist
Run Code Online (Sandbox Code Playgroud)
现在,如果我将spring-persistence.xml移动到/ test中以便我不必使用相对路径并引用它<import resource="spring-persistence.xml"/>
,那么测试运行正常.所以我认为我的XML文件的内容是可以的,但我没有正确导入相对路径.
我输入相对路径有什么明显的错误吗?也许更大的问题是,这看起来像是将applicationContext.xml分解为组件以使其更容易进行测试的合理策略吗?
谢谢!
PSh*_*tty 12
使用
<import resource="file:**/WebContent/WEB-INF/spring-persistence.xml" />
Run Code Online (Sandbox Code Playgroud)
它适用于春季3.2.1.RELEASE.旧版本我不确定.
归档时间: |
|
查看次数: |
34688 次 |
最近记录: |