我有以下示例路由流,其中我接收jms消息,构建webservice请求,然后使用webservice响应响应JMSReplyTo:
from("{{jms.input.queue}}).routeId("Receive JMS Message")
.to("direct:start");
from("direct:start").routeId("Build & Send Http Request")
.bean(processRequest)
.to("{{http.endpoint}}")
.to("direct:processResponse");
from("direct:processResponse").routeId("Build XML Response")
.convertBodyTo(String.class)
.bean(processResponse);我已经成功地对我的流程进行了单元测试,但现在我想对我的路由流程进行单元测试。我没有在测试期间运行EMS服务器,而是从第二种方法开始:
camelContext.getRouteDefinition("Build & Send Http Request").adviceWith(camelContext,
new AdviceWithRouteBuilder() {
@Override
public void configure() throws Exception {
interceptSendToEndpoint("http://*")
.skipSendToOriginalEndpoint()
.setBody("Hello");
}
});
@Test
@DirtiesContext
public void RouteFlowTest() throws Exception{
Map<String,Object> jmsHeaders = new HashMap<>();
jmsHeaders.put("Auth","helloWorld");
jmsHeaders.put("JMSReplyTo","sample");
String jmsBody = "Help Me"
incomingJmsRequestMessage.sendBodyAndHeaders("direct:start", jmsBody, jmsHeaders);
}但是现在如何在processResponse bean执行之后断言交换呢?
或者,有没有一种方法可以从第一个路由开始测试并满足JMSReplyTo的要求,而实际上不需要运行EMS服务器?
发布于 2019-04-29 20:20:27
由于您已经在编织路由,因此可以在路由建议中向模拟端点添加传播,例如:
this.weaveAddLast().to("mock:done");它只是在"Build & Send Http Request"路由的末尾添加了一个.to("mock:done")定义。从给定的问题陈述来看,.bean(processResponse);实际上是做什么的还不太清楚。您还可以将此模拟端点传播添加到"Build XML Response"路由,在这种情况下,您将需要进一步的路由建议定义。
接下来,您可以让Camel通过以下方式注入模拟端点
@EndpointInject(uri = "mock:done")
private MockEndpoint done;或者通过以下方式在测试中手动定义它:
MockEndpoint done = camelContext.getEndpoint("mock:done", MockEndpoint.class);这个模拟端点可以用于define certain expectations,这样您就可以期望该端点接收到一条消息
done.expectedMessageCount(1);
...
// invoke your route here
template.sendBody(...);
...
done.assertIsSatisfied();您还可以通过以下方向直接访问此端点接收到的交换,并对其执行进一步的断言:
Exchange exchange = done.getExchanges().get(0);
...如果您正在使用Camel on Spring (Boot),您还可以通读如何使用test Camel with Spring enabled
https://stackoverflow.com/questions/55899667
复制相似问题