unit-testing - Camel 测试 : how to get access to the headers set in the route

标签 unit-testing apache-camel

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

路线示例:

<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();

    }

}

最佳答案

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

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

关于unit-testing - Camel 测试 : how to get access to the headers set in the route,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24779750/

相关文章:

c# - 如何使用最小起订量测试调用 protected 助手的代码

xpath - Apache Camel Xpath 2.0 与 Saxon 看起来无法在 RouteBuilder/Predicates 中工作

java - Camel useOriginalMessage 不工作

spring - 多个 Camel 路线处理同一个文件

java - 为什么我不能在 groovy 中将实现作为参数传递给采用其接口(interface)的方法?

error-handling - 从onRedelivery触发新的onException路由

c++ - GoogleTest 模拟 C++ 单例

java - 对必须手动中断的异步计算进行单元测试

java - 如何使用 ExpectedException 规则在一个测试中测试多个异常?

unit-testing - Angular 2 单元测试组件,模拟 ContentChildren