骆驼模拟-MockEndpoint.whenAnyExchangeReceived处理方法未执行 [英] camel mock - MockEndpoint.whenAnyExchangeReceived process method not execute

查看:107
本文介绍了骆驼模拟-MockEndpoint.whenAnyExchangeReceived处理方法未执行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在下面有示例代码,为什么MockEndpoint.whenAnyExchangeReceived中的处理方法未执行?

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

我希望响应是来自模拟远程http调用的预期正文",但实际响应是在request(骆驼石")中传递的内容.

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

推荐答案

我在测试中添加了一些断点,看来自动创建的模拟端点是mock://http:abc/bcd,而不是mock:http://abc/bcd.

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.

要查找为什么会发生这种情况,可以查看方法org.apache.camel.impl.InterceptSendToMockEndpointStrategy#registerEndpoint,该方法称为模拟端点自动注册的一部分.从http URI中删除了//.然后到org.apache.camel.util.URISupport#normalizeUri方法,在//处添加mock uri前缀.

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.

InterceptSendToMockEndpointStrategy的实现中也有很好的注释,但是我找不到文档中提到的内容.

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

//创建模拟端点,将其用作拦截器
////从方案中替换://,以便轻松查找模拟端点,而无需在uri中添加双://

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

将其更改为getMockEndpoint("mock://http:abc/bcd")时,测试通过.

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

避免这些问题的最佳方法是,如果希望已创建端点,则将false作为getMockEndpoint()方法的第二个参数传递.如果模拟端点不存在,则将引发异常.否则,将按需创建新的模拟端点.

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.

这篇关于骆驼模拟-MockEndpoint.whenAnyExchangeReceived处理方法未执行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆