Unit testing 在调用外部模拟服务后在Camel中断言exchange属性集

Unit testing 在调用外部模拟服务后在Camel中断言exchange属性集,unit-testing,apache-camel,Unit Testing,Apache Camel,我正在尝试为camel编写一个单元测试,以检查是否根据外部模拟服务的响应正确设置了exchange属性。但是,调用模拟外部服务后,我无法访问原始exchange,因此我可以在调用后获取属性 public class OutputTest extends CamelBlueprintTestSupport { @Override protected String getBlueprintDescriptor() { return "/OSGI-INF/blueprint/blueprin

我正在尝试为camel编写一个单元测试,以检查是否根据外部模拟服务的响应正确设置了exchange属性。但是,调用模拟外部服务后,我无法访问原始exchange,因此我可以在调用后获取属性

public class OutputTest extends CamelBlueprintTestSupport {

@Override
protected String getBlueprintDescriptor() {
    return "/OSGI-INF/blueprint/blueprint-camel.xml,/OSGI-INF/blueprint/blueprint-beans.xml";
}

@Override
public boolean isUseAdviceWith() {
    return true;
}

@Override
public String isMockEndpointsAndSkip() {
    return "wmq:.*|jetty:.*";
}
@Test
public void testCallAndPropertyIsSet() throws Exception {
    getMockEndpoint("mock:jetty:http:localhost").expectedBodiesReceived(context.getTypeConverter().convertTo(String.class, new File("src/test/resources/requests/Request.xml")));
    getMockEndpoint("mock:jetty:http:localhost").returnReplyBody(new Expression() {
        @Override
        public <T> T evaluate(Exchange exchange, Class<T> aClass) {
            return context.getTypeConverter().convertTo(aClass, new File("src/test/resources/requests/Response.xml"));
        }
    });
    template.sendBody("direct:route1", context.getTypeConverter().convertTo(String.class, new File("src/test/resources/requests/ValidRequest.xml")));
    getMockEndpoint("mock:jetty:http:localhost").expectedBodiesReceived();
    //How to assert exchange property 'Property1' has been set?
}
public类OutputTest扩展了CamelBlueprintTestSupport{
@凌驾
受保护的字符串getBlueprintDescriptor(){
返回“/OSGI-INF/blueprint/blueprint camel.xml,/OSGI-INF/blueprint/blueprint beans.xml”;
}
@凌驾
公共布尔值isUseAdviceWith(){
返回true;
}
@凌驾
公共字符串isMockEndpointsAndSkip(){
返回“wmq:.*码头:.*”;
}
@试验
public void TestCallandPropertySet()引发异常{
getMockEndpoint(“mock:jetty:http:localhost”).expectedBodiesReceived(context.getTypeConverter().convertTo(String.class,新文件(“src/test/resources/requests/Request.xml”);
getMockEndpoint(“mock:jetty:http:localhost”).returnReplyBody(新表达式(){
@凌驾
公共T评估(交换,类aClass){
返回context.getTypeConverter().convertTo(aClass,新文件(“src/test/resources/requests/Response.xml”);
}
});
template.sendBody(“direct:route1”,context.getTypeConverter().convertTo(String.class,新文件(“src/test/resources/requests/ValidRequest.xml”);
getMockEndpoint(“mock:jetty:http:localhost”).expectedBodiesReceived();
//如何断言已设置exchange属性“Property1”?
}
骆驼路线蓝图:

<route id="rav">
        <from uri="direct:route1"/>
        <to uri="velocity:templates/RequestTemplate.vm"/>
        <to uri="jetty:{{integration.service.service1}}?bridgeEndpoint=true"/>
        <setProperty propertyName="Property1">
            <xpath resultType="java.lang.String">/soapenv:Envelope/soapenv:Body/namespace:element/text()</xpath>
        </setProperty>
    </route>

/soapenv:Envelope/soapenv:Body/namespace:element/text()

有不同的方法可以做到这一点。您可以尝试使用MockEndpoint并从中获取exchange。另一种方法是:

Exchange exchange = template.send("uri", new Processor() {
    public void process(Exchange exchange) throws Exception {
        exchange.getIn().setBody("");
    }
});
Message resp = exchange.getIn();
assertEquals("someproperty", resp.getProperty("propertyName"));