Camel测试:如何访问路由中设置的标头

时间:2014-07-16 11:33:39

标签: unit-testing apache-camel

我有一个Camel单元测试,我希望能够访问路由中第一个点在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();   

  }

3 个答案:

答案 0 :(得分:5)

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

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

答案 1 :(得分:1)

查看模拟端点。应该在内存中存储每个收到的交换,这样你就可以做:

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

请参阅http://camel.apache.org/mock.html

答案 2 :(得分:1)

你可以很容易地使用:

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