如何在apache camel中对servlet端点进行单元测试?

Spi*_*ina 5 java junit tomcat unit-testing apache-camel

我是Camel的新手,现在我的Tomcat服务器上运行了一条简单的路由.路由是这样构建的:

Processor generateWebResponse = new MySpecialProcessor();
from("servlet:///url?matchOnUriPrefix=true").process(generateWebResponse);
Run Code Online (Sandbox Code Playgroud)

我试过这样一个简单的单元测试:

Exchange lAuthRequest = createExchangeWithBody("[json body!]");
template.send("servlet:///url", lAuthRequest);
assertEquals("baseline body", lAuthRequest.getOut().getBody());
Run Code Online (Sandbox Code Playgroud)

但得到一个异常,表明我无法创建一个servlet端点.这是异常消息:

org.apache.camel.FailedToCreateProducerException: Failed to create Producer for endpoint: Endpoint[servlet:///url]. Reason: java.lang.UnsupportedOperationException: You cannot create producer with servlet endpoint, please consider to use http or http4 endpoint.
Run Code Online (Sandbox Code Playgroud)

这是新的发展,所以除了良好的设计之外,我没有很多限制.我愿意接受需要改变路线的建议.此外,如果我正在做一些不是惯用的事情,我很乐意用任何建议的改进来修改问题.

Cla*_*sen 7

您需要使用http客户端组件向Tomcat发送消息,例如camel-http组件:http://camel.apache.org/http

然后,您需要知道Tomcat运行servlet的端口号,例如

template.send("http://localhost:8080/myapp/myserver", lAuthRequest);
Run Code Online (Sandbox Code Playgroud)

您需要将camel-http添加到类路径中,例如,如果您使用maven,则将其添加为依赖项.