【问题标题】:Camel test: how to get access to the headers set in the route骆驼测试:如何访问路由中设置的标头
【发布时间】:2021-11-18 21:58:50
【问题描述】:

我有一个骆驼单元测试,我想访问在路由的第一个点在 Exchange 上设置的标头值。

路线示例:

<route id="VCM001_incoming">
    <from uri="file:{{InLocation}}"/>
    <convertBodyTo type="java.lang.String"/>
    <setHeader headerName="FileNameWithoutExtension">
        <simple>${file:onlyname.noext}</simple>
    </setHeader>
    <to uri="direct:splitFile"/>
</route>

使用它的 Java 代码:

public List<String> createList(Exchange exchange) {
    String fileName = (String) exchange.getIn().getHeader("FileNameWithoutExtension");

到目前为止一切都很好。

现在在我的测试中,我想知道什么是标头值“FileNameWithoutExtension”。

@Produce(uri = "file:{{InLocation}}")
private ProducerTemplate inputEndpoint;

@EndpointInject(uri = "mock:output1")
private MockEndpoint outputEndpointRPR;

@Test
public void testCamelRoute() throws Exception {
    context.addRoutes(new RouteBuilder() {
        @Override
        public void configure() throws Exception {
            from("file:{{OutLocation}}").to(outputEndpoint);
        }

    inputEndpoint.sendBody("test-message");

        Object[] expectedBodies = new Object[]{"Success: filename=xxx"};
        // At this point I need the header 'FileNameWithoutExtension' to setup the correct 'expectedBodies'

    outputEndpoint.expectedBodiesReceivedInAnyOrder(expectedBodies);

        assertMockEndpointsSatisfied();

    }

}

【问题讨论】:

    标签: unit-testing apache-camel


    【解决方案1】:

    知道这已经很晚了,对于骆驼2.15.2你可以使用以下

    outputEndpoint.expectedHeaderReceived("header", "value");
    

    【讨论】:

      【解决方案2】:

      你可以很容易地使用:

      outputEndpoint.getExchanges().get(0).getIn().getHeader("FileNameWithoutExtension");
      

      【讨论】:

        【解决方案3】:

        查看模拟端点。每个收到的交换都应该存储在内存中,以便您可以执行以下操作:

        outputEndpointRPR.getExchanges().get(0).getIn().getHeader("FileNameWithoutExtension");
        

        http://camel.apache.org/mock.html

        【讨论】:

          【解决方案4】:

          验证标头的另一种方法是:

          outputEndpoint.expectedMessagecount(1);
          outputEndpoint.message(0).header("header").isEqualTo("value");
                  
          inputEndpoint.sendBody(body);
           
          outputEndpoint.assertIsSatisfied();
          

          注意:您应该在测试设置中执行此操作!将端点放在底部会导致您的测试出现误报。

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2023-03-03
            • 2015-10-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多