簡體   English   中英

如何使用模擬的端點在駱駝測試中啟動路線

[英]how start a route in a Camel test with mocked Endpoints

我從Camel開始,在編寫測試時遇到一些問題。 我的用例與cfx代理示例完全相同。 除了我不需要“ RealWebservice”。 現在,我嘗試使用注釋方法編寫一個單元測試(而不是示例中包含的集成測試):

@RunWith(CamelSpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath:application-context.xml" })
@MockEndpointsAndSkip
public class RoutesTest {

@Autowired
CamelContext camelContext;

@EndpointInject(uri = "mock:cxf:bean:cxfEndpoint", context = "camelContext")
MockEndpoint cxfEndpoint;

@EndpointInject(uri = "mock:log:input", context = "camelContext")
MockEndpoint logInputEndpoint;

@EndpointInject(uri = "mock:http:realhostname:8211/service", context = "camelContext")
MockEndpoint realEndpoint;

@EndpointInject(uri = "mock:cxf:bean:cxfEndpoint")
ProducerTemplate producer;

@Test
public void testLeleuxMifidRoute() throws InterruptedException {
    String body = "<blah/>";

    cxfEndpoint.expectedBodiesReceived(body);
    logInputEndpoint.expectedBodiesReceived(body);
    realEndpoint.expectedBodiesReceived(body);

    producer.sendBody(body);

    MockEndpoint.assertIsSatisfied(camelContext);
}
}

cxfEndpoint接收到該消息, 但其他端點未收到

路由如下所示(當我運行它並使用SoapUI發送消息時,它可以工作,顯然,在此示例中,我混淆了ips和beannames):

<endpoint id="callRealWebService" uri="http://realhostname:8211/service?throwExceptionOnFailure=true" /> 
<route>
  <from uri="cxf:bean:cxfEndpoint?dataFormat=MESSAGE"/>
  <to uri="log:input?showStreams=true"/>
  <to ref="callRealWebService"/>
  <to uri="log:output"/>
</route>

我究竟做錯了什么? 我發現的所有示例和其他問題似乎都使用“ direct:start”或更改生產路線。

我們成功使用的一種方法是為測試執行和主代碼提供不同的屬性文件。

我們在駱駝上下文中定義屬性

<propertyPlaceholder id="properties"
            location="classpath:META-INF/uri.properties" xmlns="http://camel.apache.org/schema/spring" />

在文件夾/src/main/resources/META-INF/我們有uri.properties文件用於主代碼,在/src/test/resources/META-INF/我們具有uri.properties用於測試執行。

必須使用符號{{properties.name}}使用屬性占位符而不是實際uri值來重寫路由。

<route>
  <from uri="{{cxf.bean.cxfEndpoint}}"/>
</route>

主要的uri.properties將是

cxf.bean.cxfEndpoint=cxf:bean:cxfEndpoint?dataFormat=MESSAGE

測試uri.properties將是

cxf.bean.cxfEndpoint=direct:start

使用此配置,您將能夠輕松測試您的路線。

暫無
暫無

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

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