我有一个 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/