2011-03-28 111 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但不得不求助于在溶液中描述的手动方法...