2011-03-28 55 views
3

我有一個在CamelRoutes.xml中定義的路由,我想通過使用http://camel.apache.org/mock.html底部描述的包裝技術來測試它們。使用彈簧配置測試駱駝的問題

CamelRoutes.xml

<route autoStartup="true" xmlns="http://camel.apache.org/schema/spring"> 
     <from uri="direct:start"/> 
     <to uri="direct:end"/> 
    </route> 

因此,我創建CamelRoutesTest.xml包含:

<import resource="CamelRoutes.xml"/> 
<bean id="mockAllEndpoints" class="org.apache.camel.impl.InterceptSendToMockEndpointStrategy"/> 

,但我不知道怎麼說都加載在Spring的XML,並提供訪問模擬端點創建一個測試。

如果我使用..

@ContextConfiguration(locations=("/CamelRoutesTest")) 
public class CamelTest extends AbstractJUnit38SpringContextTests 

} 

然後我不知道如何得到模擬端點

如果我使用..

public class CamelTest extends CamelTestSupport 

} 

然後我不知道如何加載我的駱駝環境..

我似乎無法找到一個例子在使用CamelTestSupport的網站上進行測試,並從spring xml加載路由。

回答

3

解決使用:

public class CamelTest extends CamelSpringTestSupport { 

    @Override 
    protected AbstractXmlApplicationContext createApplicationContext() { 
     return new ClassPathXmlApplicationContext("/CamelRoutesTest.xml"); 
    } 

    @Test 
    @DirtiesContext 
    public void discover() throws Exception { 

     getMockEndpoint("mock:my.route.out").expectedMessageCount(1); 

     template.sendBody("direct:my.route.in", FileUtils.slurpClassPathFile("/samples/sample.data.xml")); 

     assertMockEndpointsSatisfied(); 

    } 


} 
3

Tom你已經在駱駝郵件列表上發佈了這個。我建議你在這裏發佈Q時也寫下這個。

答案已經張貼在這裏 http://camel.465427.n5.nabble.com/Problems-testing-Camel-with-Spring-config-tp4267754p4267754.html

+0

我常在這裏如果更新我的問題/當我得到答案的其他地方。目前,我仍然缺乏一個體面的例子來說明如何正確地模擬我的路線,以便我的測試不會觸及外部系統。 – Tom 2011-03-29 07:53:17

2

感謝這個解決方案,@湯姆!

我也很難讓Camel使用我的Spring-powered JUnit測試,問題是建議的CamelSpringJUnit4ClassRunner現在已經從camel-spring.jar移動到一個單獨的模塊camel-spring-test.jar,它在2.9.2標準駝峯分佈中不可用。

所以我不能使用@RunWith但不得不求助於在溶液中描述的手動方法...