我有 2 个项目。包含一些组件 (POJO) 的插件项目和包含相应单元和集成测试的片段项目。我正在使用 Tycho 来构建这些项目,并且我想使用 Spring 来引导我的集成测试。
我用注释注释了我的测试类
@ContextConfiguration(locations = { "classpath*:spring/*-config.xml" })
@RunWith(SpringJUnit4ClassRunner.class)
但是当我尝试使用 tycho 构建项目(clean install
)或在 eclipse 中将测试类作为 Plugin-Test 运行时,Spring 提示定义的上下文中没有 bean。在日志中我发现以下几行:
DEBUG o.s.t.c.s.AbstractGenericContextLoader - Loading ApplicationContext for
locations [classpath*:spring/*-config.xml].
DEBUG o.s.b.f.xml.XmlBeanDefinitionReader - Loaded 0 bean definitions from
location pattern [classpath*:spring/*-config.xml]
我已将配置文件放在 src/main/java/spring/
和 src/main/resources/spring
下,但 spring 找不到它们。我还尝试将这些路径显式添加到 list 中的包类路径中。
当我将配置路径更改为“file:spring/some-config.xml”
时,spring正在加载我的bean定义,但当它尝试使用以下输出加载“上下文”模式时崩溃:
Configuration problem: Unable to locate Spring NamespaceHandler for XML schema
namespace [http://www.springframework.org/schema/context]
为什么它不能与类路径前缀一起使用?为什么它与文件前缀一起使用?我认为文件前缀仅适用于文件系统,不适用于 jar 文件...我做错了什么?
提前致谢
更新:以下是(片段)测试项目的完整 View :
/
+-- src/main/java/
| +-- MyTestClass.java
|
+-- src/main/resources/
| +-- spring/
| | +-- some-config.xml
| +-- log4j.properties
|
+-- META-INF/
| +-- MANIFEST.MF
|
+-- pom.xml
第谷尝试执行我的测试类后,我在目标下看到以下文件:
/target
|
+-- classes/
+-- MyTestClass.class
+-- spring/
+-- some-config.xml
+-- log4j.properties
+-- work/ // contains the eclipse configuration (config.ini, etc.)
+-- MANIFEST.MF
+-- mybundle-xx.jar
我省略了属性和 surfire 文件。 target/work/configuration/下生成的 config.ini 列出了 list 中作为必需包提及的所有包。除了我的测试片段包之外,它们都被引用为 jar 文件。对于测试包,存在以下条目:
reference\:file\:C\:/[...]/workspaces/workspace/my.bundle.tests
这是正确的吗?它至少可以解释为什么文件前缀有效......
但是类路径前缀呢? list 是否已复制到目标文件夹中的正确位置?我的意思是它位于 dev.properties
中引用的 classes
文件夹之外。
此外,log4j 在启动时提示它没有正确配置,这表明它在类路径上找不到 log4j.properties。
更新:现在我正在尝试另一种方法。我读过this article这似乎是一种更简单的让事情运转的方法。因此,我将 maven-surfire-plugin 添加到我的 pom 中,并将打包类型从“eclipse-test-plugin”更改为“jar”,以便 tycho 不会运行它自己的 Surefire-plugin。但现在我又面临着另一个问题。 Spring 似乎只为 maven2 存储库提供 ArtifactLocator,而不是像 tycho 使用的 p2 存储库。
有谁知道是否有 p2 存储库的 ArtifactLocator?
有人使用与 tycho、osgi 和 spring 相同的设置进行集成测试吗?
最佳答案
将 spring-context-xx.jar
放入类路径中。
命名空间由 NamespaceHandler
接口(interface)的实现处理。在启动时,spring 加载所有这些,并尝试使用加载的处理程序解析每个 namespace 。如果没有一个声称能够解析它,则抛出异常。 context:
命名空间由位于上述 jar 中的 ContextNamespaceHandler
解析。
关于java - Spring应用程序上下文无法加载配置文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7787160/