我有一个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();
}
}
知道这已经很晚了,对于camel 2.15.2,您可以使用以下
outputEndpoint.expectedHeaderReceived("header", "value");
你可以很容易地使用:
outputEndpoint.getExchanges().get(0).getIn().getHeader("FileNameWithoutExtension");
查看模拟端点。应该将每个接收到的交换存储在内存中,以便您可以执行以下操作:
outputEndpointRPR.getExchanges().get(0).getIn().getHeader("FileNameWithoutExtension");
见http://camel.apache.org/mock.html
另一种验证header的方法是:
outputEndpoint.expectedMessagecount(1);
outputEndpoint.message(0).header("header").isEqualTo("value");
inputEndpoint.sendBody(body);
outputEndpoint.assertIsSatisfied();
注意:您应该在测试的设置中这样做!将端点放在底部将导致测试误报。