Camel test: how to get access to the headers set in the route

apache-camel, unit-testing

Solution

You could get it quite easily using:

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

Problem

I have a Camel Unit Test and I want to get access to the header values that are set on the Exchange on the first point in the route. Route example : ``` <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 code where it's used : ``` public List<String> createList(Exchange exchange) { String fileName = (String) exchange.getIn().getHeader("FileNameWithoutExtension"); ``` So all good to this point. Now in my test I want to find out what header value is "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(); } } ```

Original source