【问题标题】:Test Camel Integration with JUnit使用 JUnit 测试 Camel 集成
【发布时间】:2014-08-14 07:56:39
【问题描述】:

我有一个 Camel 集成流程。在某一时刻,有客户端创建,如下配置:

<routeContext id="clientCreation" xmlns="http://camel.apache.org/schema/spring">
    <route customId="true" id="clientCreationRouter">
        <from uri="direct:client-creation-start" customId="true" id="Client-Creation-Initiator"/>
        <bean ref="clientCreationService" method="constructClientDto" />
        <bean ref="clientCreationService" method="populateClientRequestMessage" />
        <bean ref="clientCreationService" method="saveClientDetails" />
        <bean ref="clientCreationService" method="createClientCreationFixedLengthMessage" />
        <setHeader headerName="flow-status" customId="true" id="Client-Creation-Update-Header">
             <simple>CLIENT_CREATED</simple>
        </setHeader>
    </route>
</routeContext>

ConstructClientDto 有 FlowInfo 对象作为输入。

createClientCreationFixedLengthMessage 有一个String 作为输出。我想测试这部分流程。基本上,我想提供一个 FlowInfo 对象,该对象已准备好进行客户端创建,然后根据最后提供的结果查看这部分集成是否成功。

到目前为止,这是我尝试过的:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration (locations={"/META-INF/spring/data-extractor.xml",
        "/META-INF/spring/data-splitter.xml",
        "/META-INF/spring/client-creation.xml"})
public class ClientCreationFlowTest extends CamelTestSupport{

    @Produce (uri = "direct:client-creation-start")
    protected ProducerTemplate template;

    @EndpointInject(uri = "mock:result")
    protected MockEndpoint resultEndpoint;

    @Autowired
    FlowRepository repository;

    @Test
    public void testClientCreation () throws Exception {
        FlowInfo flowInfo = repository.findById("1234");
        template.sendBody(resultEndpoint, flowInfo);
    }
}

当我运行测试用例时,我得到了成功。但是当我调试它时,它似乎并没有通过ClientCreationService 运行。我对骆驼很陌生,任务很关键。任何帮助,将不胜感激。

【问题讨论】:

    标签: junit apache-camel integration-testing


    【解决方案1】:

    首先,在您的路由定义中,您必须在最后将结果发送到mock:result:

    <routeContext id="clientCreation" xmlns="http://camel.apache.org/schema/spring">
        <route customId="true" id="clientCreationRouter">
            <from uri="direct:client-creation-start" customId="true" id="Client-Creation-Initiator"/>
            <bean ref="clientCreationService" method="constructClientDto" />
            <bean ref="clientCreationService" method="populateClientRequestMessage" />
            <bean ref="clientCreationService" method="saveClientDetails" />
            <bean ref="clientCreationService" method="createClientCreationFixedLengthMessage" />
            <setHeader headerName="flow-status" customId="true" id="Client-Creation-Update-Header">
                 <simple>CLIENT_CREATED</simple>
            </setHeader>
            <to uri="mock:result"/>  
        </route>
    </routeContext>
    

    其次,在您的 JUnit 测试类中,您不能将消息发送到模拟端点,而是发送到 direct:client-creation-start,这是自动完成的,因为模板与此 uri 自动装配:

    @RunWith(SpringJUnit4ClassRunner.class)
    @ContextConfiguration (locations={"/META-INF/spring/data-extractor.xml",
            "/META-INF/spring/data-splitter.xml",
            "/META-INF/spring/client-creation.xml"})
    public class ClientCreationFlowTest extends CamelTestSupport{
    
        @Produce(uri = "direct:client-creation-start")
        protected ProducerTemplate template;
    
        @EndpointInject(uri = "mock:result")
        protected MockEndpoint resultEndpoint;
    
        @Autowired
        FlowRepository repository;
    
        @Test
        public void testClientCreation () throws Exception {
            FlowInfo flowInfo = repository.findById("1234");
    
            resultEndpoint.expectedBodiesReceived("my string output");
            template.sendBody(flowInfo);
            resultEndpoint.assertIsSatisfied();
        }   
    }
    

    【讨论】:

      【解决方案2】:

      尝试template.sendBody(flowInfo);,而不是直接将其发送到结果端点,这会绕过你的整个路由。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-03-14
        • 2018-07-29
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多