我正在创建一个由 Camel 路由组成的 ServiceMix 模块。
在我的 beans.xml 中,我有:
<osgix:cm-properties id="companyProps"
persistent-id="com.company.integration">
</osgix:cm-properties>
<ctx:property-placeholder location="
file:${karaf.base}/etc/com.company.integration.cfg
" />
这意味着我可以使用文件中的属性定义其他项目,例如:
<http-conf:conduit name="*.http-conduit">
<http-conf:authorization>
<security:UserName>${username}</security:UserName>
<security:Password>${password}</security:Password>
</http-conf:authorization>
</http-conf:conduit>
如果我创建一个 bean 并注入(inject)它,我还可以访问我的 java 类中的属性:
<bean id="myConfig" class="com.company.integration.MyConfig">
<osgix:managed-properties persistent-id="com.company.integration" />
</bean>
问题出在我编写单元测试时。目前我正在使用带有测试值的 beans.xml 副本,但我当然想使用真正的 beans.xml 并为属性提供值。
public class myTest extends CamelSpringTestSupport
{
@Override
protected AbstractXmlApplicationContext createApplicationContext()
{
return new ClassPathXmlApplicationContext(new String[] {
"/META-INF/spring/beans.xml"
, "/META-INF/spring/test.xml"
});
}
}
我想摆脱 test.xml,最好从属性文件中加载测试属性。 我已经看到一些对 PropertyComponent 的引用,但我无法让它工作:-(
最佳答案
在我们的组织中,我们将 spring-osgi 配置与 spring bean 配置分开。
在我们的 spring-osgi 配置中,我们将具有以下内容:
<osgix:cm-properties id="companyProps" persistent-id="com.company.integration">
<prop key="name">value</prop>
<prop key="name">value</prop>
...
</osgix:cm-properties>
以及任何特定于 osgi 的配置(例如服务注册、事件等)
在我们的 spring-bean 配置中,我们定义了我们可以在 osgi 容器之外使用的所有 beans,包括 camel 路由/上下文等。
对于我们的测试,我们使用我们的 spring bean 定义和包含以下内容的测试 spring 配置启动一个 ClassPathXmlContext:
<ctx:property-placeholder location="classpath*:test.properties" />
以及我们需要的 osgi 特定 bean 的任何模拟。
希望这对您有所帮助。
关于java - 如何在测试基于 CamelSpringTestSupport 的测试期间设置属性值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9992492/