我正在尝试使用CamelTestSupport测试我的Camel路由。我在类似
的类中定义了我的路由public class ActiveMqConfig{
@Bean
public RoutesBuilder route() {
return new SpringRouteBuilder() {
@Override
public void configure() throws Exception {
from("activemq:{{push.queue.name}}").to("bean:PushEventHandler?method=handlePushEvent");
}
};
}
}
我的测试类看起来像这样
@RunWith(SpringRunner.class)
public class AmqTest extends CamelTestSupport {
@Override
protected RoutesBuilder createRouteBuilder() throws Exception {
return new ActiveMqConfig().route();
}
@Override
protected Properties useOverridePropertiesWithPropertiesComponent() {
Properties properties = new Properties();
properties.put("pim2.push.queue.name", "pushevent");
return properties;
}
protected Boolean ignoreMissingLocationWithPropertiesComponent() {
return true;
}
@Mock
private PushEventHandler pushEventHandler;
@BeforeClass
public static void setUpClass() throws Exception {
BrokerService brokerSvc = new BrokerService();
brokerSvc.setBrokerName("TestBroker");
brokerSvc.addConnector("tcp://localhost:61616");
brokerSvc.setPersistent(false);
brokerSvc.setUseJmx(false);
brokerSvc.start();
}
@Override
protected JndiRegistry createRegistry() throws Exception {
JndiRegistry jndi = super.createRegistry();
MockitoAnnotations.initMocks(this);
jndi.bind("pushEventHandler", pushEventHandler);
return jndi;
}
@Test
public void testConfigure() throws Exception {
template.sendBody("activemq:pushevent", "HelloWorld!");
Thread.sleep(2000);
verify(pushEventHandler, times(1)).handlePushEvent(any());
}}
这工作得非常好。但我必须使用{{push.queue.name}}
函数设置占位符useOverridePropertiesWithPropertiesComponent
。但是我想从我的.yml文件中读取它
我无法做到。有人可以建议。
由于
答案 0 :(得分:1)
通常从.properties文件中读取属性。但是您可以编写一些代码来读取useOverridePropertiesWithPropertiesComponent
方法中的yaml文件,并将它们放入返回的Properties
实例中。
答案 1 :(得分:0)
感谢克劳斯 我通过这样做得到了它
@Override
protected Properties useOverridePropertiesWithPropertiesComponent() {
YamlPropertySourceLoader loader = new YamlPropertySourceLoader();
try {
PropertySource<?> applicationYamlPropertySource = loader.load(
"properties", new ClassPathResource("application.yml"),null);
Map source = ((MapPropertySource) applicationYamlPropertySource).getSource();
Properties properties = new Properties();
properties.putAll(source);
return properties;
} catch (IOException e) {
LOG.error("Config file cannot be found.");
}
return null;
}