我在Apache Camel上使用了3-4个月的Spring 4.0.7.RELEASE,我有几个基于扩展CamelTestSupport的Camel 2.14.0 TestNG测试,其中我使用了一些MockEndpoint,我通过重写createRouteBuilder()方法配置了我的路由。
现在,我还需要通过@Autowired注释将一些Spring注入其中之一。通过阅读http://camel.apache.org/spring-testing.html中的内容,我理解了我现在必须扩展AbstractCamelTestNGSpringContextTests,它支持@Autowired、@DirtiesContext和@ContextConfiguration。
虽然我知道所有的MockEndpoint都不是getMockEndpoint()方法可以访问的,但是通过使用@EndpointInject注释,我不清楚如何表达我的路由,因为createRouteBuilder()没有更多的可用。
我看到通过使用注释来定义生产者和消费者是可能的,但我无法理解如何设计路由。
多谢社会人士。
发布于 2014-10-02 20:59:34
对于给定的这里解决方案,如果您想要初始化带注释的Spring配置上下文而不需要额外的XML配置文件,则可以结合使用TestNG助手CamelSpringTestSupport和AnnotationConfigApplicationContext。
使用Spring注释的Camel配置bean类:
@Configuration
public class MyConfig extends SingleRouteCamelConfiguration {
@Bean
@Override
public RouteBuilder route() {
return new RouteBuilder() {
@Override
public void configure() throws Exception {
from("direct:test").to("mock:direct:end");
}
};
}
}TestNG测试类扩展了CamelSpringTestSupport,Spring配置MyConfig由AnnotationConfigApplicationContext初始化
public class TestNGTest extends org.apache.camel.testng.CamelSpringTestSupport {
@EndpointInject(uri = "mock:direct:end")
protected MockEndpoint errorEndpoint;
@Produce(uri = "direct:test")
protected ProducerTemplate testProducer;
@Override
protected AbstractApplicationContext createApplicationContext() {
return new AnnotationConfigApplicationContext(MyConfig.class);
}
@DirtiesContext
@Test
public void testRoute() throws InterruptedException {
// use templates and endpoints
}
}https://stackoverflow.com/questions/26158049
复制相似问题