调用外部模拟服务后,在Camel中设置断言交换属性

时间:2016-06-02 07:51:33

标签: unit-testing apache-camel

我试图为camel编写单元测试,以检查是否根据外部模拟服务的响应正确设置了交换属性。但是我可以在通话结束后获得该房产,因为在调用模拟外部服务后我无法访问原始交易所。

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?
}

蓝图骆驼路线:

<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>

1 个答案:

答案 0 :(得分:0)

有不同的方法可以做到这一点。您可以尝试使用MockEndpoint并从那里进行交换。另一种方法是:

     Exchange exchange = template.send("uri", new  new Processor() {
          public void process(Exchange exchange) throws Exception {
            exchange.getIn().setBody("");
          }
        });

     Message resp = exchange.getIn();
     assertEquals("someproperty", resp.getProperty("propertyName");