我的 Camel 上下文中有这样的路线:-
<camel:route id="xxx">
<camel:from uri="direct:test"></camel:from>
<camel:to uri="bean:testProcessor"></camel:to>
<camel:to uri="{{testUrl}}"></camel:to>
<camel:to uri="bean:responseHandler"></camel:to>
</camel:route>
我想测试整个路由。所以每当我向 direct:test 发送请求时,它都会调用 testProcessor,然后使用 testUrl 调用 http 服务,然后调用 responseHandler bean 元素。我该如何测试这个?最重要的是这里 stub http 服务
最佳答案
我在理解您的确切用例时遇到了一些困难,因此我将向您指出可用于测试的 Camel 的 AdviceWithRouteBuilder 库。我对基于 Camel xml 的版本不是 100% 流畅,所以我将在我的示例中使用 Java DSL。以下是一些 Camel 文档的链接,您可以将其用作引用: http://camel.apache.org/advicewith.html
//示例路线
from("direct:myNormalInput").routeId("xxx")
.to("myBean", "myMethod").id("enrichmentBean")
.to("http://myawesomeurl").id("HttpCaller")
.to("myResponseBean", "myMethod").id("responseHandler");
//示例单元测试
public void myTest throws Exception {
context.getRouteDefinition("xxx").adviceWith(context, new AdviceWithRouteBuilder() {
//You can replace your normal route's from statement
replaceFromWith("direct:testEntry");
//Swap out your enrichmentBean with a replace or remove it if you prefer
weaveById("enrichmentBean").replace().to("myTestBean", "myTestMethod");
//mock out your http call with a different url or a fake endpoint
weaveById("HttpCaller").replace().to("http://myTestUrl");
//extract your message at any point in processing to do some validation work
weaveById("responseHandler").after().to("mock:extract");
}
context.start();
template.sendBody("direct:testEntry", "myTestBody");
MockEndpoint test = getMockEndpoint("mock:extract");
int messageCount = test.getReceivedExchanges().size();
assertEquals(1, messageCount);
}
关于java - Camel junit 使用bean组件测试http组件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37290910/