簡體   English   中英

如何在apache camel中對servlet端點進行單元測試?

[英]How do you unit test a servlet endpoint in apache camel?

我是Camel的新手,現在我的Tomcat服務器上運行了一條簡單的路由。 路由是這樣構建的:

Processor generateWebResponse = new MySpecialProcessor();
from("servlet:///url?matchOnUriPrefix=true").process(generateWebResponse);

我試過這樣一個簡單的單元測試:

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

但得到一個異常,表明我無法創建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.

這是新的發展,所以除了良好的設計之外,我沒有很多限制。 我願意接受需要改變路線的建議。 此外,如果我正在做一些不是慣用的事情,我很樂意用任何建議的改進來修改問題。

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

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

template.send("http://localhost:8080/myapp/myserver", lAuthRequest);

您需要將camel-http添加到類路徑中,例如,如果您使用maven,則將其添加為依賴項。

我通過將路線分成兩部分來解決我的問題。 現在路由聲明如下所示:

from("servlet:///auth?matchOnUriPrefix=true").inOut("direct:auth");
from("direct:auth").process(new AuthorizationProcessor());

測試看起來像這樣:

Exchange lAuthRequest = createExchangeWithBody("test body");
template.send("direct:auth", lAuthRequest);
assertEquals("processed body", lAuthRequest.getOut().getBody());

這不是一個完整的測試,但允許我覆蓋除傳入的servlet部分之外的所有路由。 我認為現在已經足夠了。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM