camel mock - MockEndpoint.whenAnyExchangeReceived process method not execute

余生长醉 提交于 2019-12-24 09:49:47

问题


I have example code below, why is the process method in MockEndpoint.whenAnyExchangeReceived NOT executed?

I expect the response is "Expected Body from mock remote http call", but the actual response is what passed in request("Camel rocks").

public class CamelMockRemoteHttpCallTest extends CamelTestSupport {

    @Override
    protected RouteBuilder createRouteBuilder() throws Exception {
        return new RouteBuilder() {
            @Override
            public void configure() throws Exception {
                from("direct:start")
                    .to("http://abc/bcd")
                    ;
            }
        };
    }

    @Override
    public String isMockEndpointsAndSkip() {
        return "http://abc/bcd";
    }

    @Test
    public void testSimulateErrorUsingMock() throws Exception {
        MockEndpoint http = getMockEndpoint("mock:http://abc/bcd");

        http.whenAnyExchangeReceived(new Processor() {
            public void process(Exchange exchange) throws Exception {
                exchange.getOut().setBody("Expected Body from mock remote http call");   //why this line doesn't execute
            }
        });

        String response = template.requestBody("direct:start", "Camel rocks", String.class);

        assertEquals("Expected Body from mock remote http call", response);  //failed, the actual response is "Camel rocks"
    }
}

回答1:


I have added some breakpoints to your test and it seems, that automatically created mock endpoint is mock://http:abc/bcd, not mock:http://abc/bcd.

To find, why is this happening, you can look to method org.apache.camel.impl.InterceptSendToMockEndpointStrategy#registerEndpoint, which is called as part of mock endpoint auto registration. There is // removed from http URI. And then to org.apache.camel.util.URISupport#normalizeUri method, where is // added for mock uri prefix.

There is also nice comment in implementation of InterceptSendToMockEndpointStrategy, but I couldn't find it mentioned in documentation.

// create mock endpoint which we will use as interceptor
// replace :// from scheme to make it easy to lookup the mock endpoint without having double :// in uri

When you change it to getMockEndpoint("mock://http:abc/bcd"), the test passes.

The best way to avoid these issues, is pass false as second parameter of getMockEndpoint() method, if you expect already created endpoint. This will throw exception, if mock endpoint does not exists. Otherwise is new mock endpoint created on demand.



来源:https://stackoverflow.com/questions/50201617/camel-mock-mockendpoint-whenanyexchangereceived-process-method-not-execute

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!