[英]Camel junit test http component with bean component
I have route in my camel context like this:- 我在骆驼语境中有这样的路线:-
<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>
i want to test the whole route .so whenever i sent request to direct:test it will call the testProcessor and then calls the http service with testUrl and then calls the responseHandler bean element. 我想测试整个路由。因此,每当我发送对direct:test的请求时,它将调用testProcessor,然后使用testUrl调用http服务,然后调用responseHandler bean元素。 How can i test this?
我该如何测试? Most importantly here stubbing the http service
最重要的是在这里存根http服务
I have having some trouble understanding your exact use case so I will point you towards camel's AdviceWithRouteBuilder library you can use for testing. 我在理解您的确切用例时遇到了一些麻烦,因此我将向您介绍可用于测试的骆驼的AdviceWithRouteBuilder库。 I am not 100% fluid in the camel xml based version so I will use Java DSL for my sample.
在基于骆驼xml的版本中,我不是百分百地适应,因此我将使用Java DSL作为示例。 Here is the link to some camel documentation you can use as a reference: http://camel.apache.org/advicewith.html
这是一些骆驼文档的链接,您可以将其用作参考: http : //camel.apache.org/advicewith.html
//sample route //示例路线
from("direct:myNormalInput").routeId("xxx")
.to("myBean", "myMethod").id("enrichmentBean")
.to("http://myawesomeurl").id("HttpCaller")
.to("myResponseBean", "myMethod").id("responseHandler");
//Sample Unit Test //样本单元测试
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);
}
You can also add a route with Jetty consumer on the fly in your unit tests. 您还可以在单元测试中动态添加Jetty消费者的路线。 This way, you can actually test the web service behaviour.
这样,您实际上可以测试Web服务行为。 I tend to do this when writing integration tests and can be very useful.
在编写集成测试时,我倾向于这样做,并且可能非常有用。 Otherwise, as Matthew suggested, you can just mock it out.
否则,如Matthew所建议的,您可以将其模拟出来。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.