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

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

问题描述

我有下面的示例代码,为什么 MockEndpoint.whenAnyExchangeReceived 中的 process 方法没有执行?

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

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

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.

//创建我们将用作拦截器的模拟端点
//替换 ://from 方案,以便在 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天全站免登陆