2013-01-15 32 views
8

我從駱駝開始,我在編寫測試時遇到了一些問題。我的使用案例與cfx proxy example完全一樣。除了我不需要「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> 

我在做什麼錯?我發現的所有示例和其他問題似乎都使用「直接:開始」或更改生產路線。

回答

4

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

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

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

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

你的路由具有使用屬性的佔位符,而不是真正的URI值使用符號{{properties.name}}

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

被改寫,主要uri.properties將

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

測試uri.properties將是

cxf.bean.cxfEndpoint=direct:start 

使用此配置ñ,你將能夠輕鬆測試你的路線。