【问题标题】:Problems testing Camel with Spring config使用 Spring 配置测试 Camel 的问题
【发布时间】:2012-05-25 21:02:56
【问题描述】:

我在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 加载路由的示例测试。

【问题讨论】:

    标签: unit-testing spring apache-camel


    【解决方案1】:

    汤姆,你已经在 Camel 邮件列表上发布了这个。我建议你在这里发布 Q 时也写这个。

    答案已经发布在这里 http://camel.465427.n5.nabble.com/Problems-testing-Camel-with-Spring-config-tp4267754p4267754.html

    【讨论】:

    • 如果/当我在其他地方得到答案时,我通常会在这里更新我的问题。目前,我仍然缺乏一个很好的例子来说明如何正确模拟我的路线,以便我的测试不会影响外部系统。
    【解决方案2】:

    解决方法:

    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!

      我也很难让 Camel 使用 Spring 驱动的 JUnit 测试,问题是建议的 CamelSpringJUnit4ClassRunner 现在已从 camel-spring.jar 移动到单独的模块 camel-spring-test.jar,即自 2.9.2 起,标准骆驼分发版中不可用。

      所以我不能使用@RunWith,而是不得不求助于解决方案中描述的手动方法......

      【讨论】:

        猜你喜欢
        • 2015-12-29
        • 1970-01-01
        • 2011-12-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-12-26
        相关资源
        最近更新 更多