首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >从CamelTestSupport迁移到AbstractCamelTestNGSpringContextTests

从CamelTestSupport迁移到AbstractCamelTestNGSpringContextTests
EN

Stack Overflow用户
提问于 2014-10-02 09:32:36
回答 1查看 1.4K关注 0票数 1

我在Apache Camel上使用了3-4个月的Spring 4.0.7.RELEASE,我有几个基于扩展CamelTestSupportCamel 2.14.0 TestNG测试,其中我使用了一些MockEndpoint,我通过重写createRouteBuilder()方法配置了我的路由。

现在,我还需要通过@Autowired注释将一些Spring注入其中之一。通过阅读http://camel.apache.org/spring-testing.html中的内容,我理解了我现在必须扩展AbstractCamelTestNGSpringContextTests,它支持@Autowired@DirtiesContext@ContextConfiguration

虽然我知道所有的MockEndpoint都不是getMockEndpoint()方法可以访问的,但是通过使用@EndpointInject注释,我不清楚如何表达我的路由,因为createRouteBuilder()没有更多的可用。

我看到通过使用注释来定义生产者和消费者是可能的,但我无法理解如何设计路由。

多谢社会人士。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-10-02 20:59:34

对于给定的这里解决方案,如果您想要初始化带注释的Spring配置上下文而不需要额外的XML配置文件,则可以结合使用TestNG助手CamelSpringTestSupportAnnotationConfigApplicationContext

使用Spring注释的Camel配置bean类:

代码语言:javascript
运行
复制
@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配置MyConfigAnnotationConfigApplicationContext初始化

代码语言:javascript
运行
复制
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
    }
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/26158049

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档