CamelTestSupport 从 yml 文件中读取占位符
CamelTestSupport read placeholders from yml file
我正在尝试使用 CamelTestSupport 测试我的 Camel Routes。我在这样的 class 中定义了我的路线
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");
}
};
}
}
我的测试 class 看起来像这样
@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());
}}
这工作得很好。但是我必须使用 useOverridePropertiesWithPropertiesComponent
函数设置占位符 {{push.queue.name}}
。但我希望从我的 .yml 文件中读取它。
我做不到。谁能推荐一下。
谢谢
属性通常从 .properties 文件中读取。但是您可以编写一些代码来读取 useOverridePropertiesWithPropertiesComponent
方法中的 yaml 文件,并将它们放入返回的 Properties
实例中。
谢谢克劳斯
我通过这样做让它工作
@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;
}
我正在尝试使用 CamelTestSupport 测试我的 Camel Routes。我在这样的 class 中定义了我的路线
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");
}
};
}
}
我的测试 class 看起来像这样
@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());
}}
这工作得很好。但是我必须使用 useOverridePropertiesWithPropertiesComponent
函数设置占位符 {{push.queue.name}}
。但我希望从我的 .yml 文件中读取它。
我做不到。谁能推荐一下。
谢谢
属性通常从 .properties 文件中读取。但是您可以编写一些代码来读取 useOverridePropertiesWithPropertiesComponent
方法中的 yaml 文件,并将它们放入返回的 Properties
实例中。
谢谢克劳斯
我通过这样做让它工作
@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;
}