37.9. 模拟现有端点


Camel 现在允许您自动模拟 Camel 路由中的现有端点。

注意


如何工作。端点仍在操作中。另一种情况是,注入 Mock 端点并接收消息,然后将消息委派给目标端点。您可以将此操作视为截获类型,并委派或端点监听程序。

假设您有以下给定的路由:

Route(路由)

    @Override
    protected RouteBuilder createRouteBuilder() throws Exception {
        return new RouteBuilder() {
            @Override
            public void configure() throws Exception {
                from("direct:start").routeId("start")
                        .to("direct:foo").to("log:foo").to("mock:result");

                from("direct:foo").routeId("foo")
                        .transform(constant("Bye World"));
            }
        };
    }
Copy to Clipboard Toggle word wrap

然后,您可以使用 Camel 中的 建议 带功能来模拟来自单元测试给定路由中的所有端点,如下所示:

提供模拟所有端点的建议

    @Test
    public void testAdvisedMockEndpoints() throws Exception {
        // advice the start route using the inlined AdviceWith lambda style route builder
        // which has extended capabilities than the regular route builder
        AdviceWith.adviceWith(context, "start", a ->
        // mock all endpoints
        a.mockEndpoints());

        getMockEndpoint("mock:direct:start").expectedBodiesReceived("Hello World");
        getMockEndpoint("mock:direct:foo").expectedBodiesReceived("Hello World");
        getMockEndpoint("mock:log:foo").expectedBodiesReceived("Bye World");
        getMockEndpoint("mock:result").expectedBodiesReceived("Bye World");

        template.sendBody("direct:start", "Hello World");

        assertMockEndpointsSatisfied();

        // additional test to ensure correct endpoints in registry
        assertNotNull(context.hasEndpoint("direct:start"));
        assertNotNull(context.hasEndpoint("direct:foo"));
        assertNotNull(context.hasEndpoint("log:foo"));
        assertNotNull(context.hasEndpoint("mock:result"));
        // all the endpoints was mocked
        assertNotNull(context.hasEndpoint("mock:direct:start"));
        assertNotNull(context.hasEndpoint("mock:direct:foo"));
        assertNotNull(context.hasEndpoint("mock:log:foo"));
    }
Copy to Clipboard Toggle word wrap

请注意,模拟端点被赋予了 URI Vulnerability:& lt;endpoint>,如 模拟:direct:fooINFO 级别的 Camel 日志是模拟的端点:

INFO  Adviced endpoint [direct://foo] with mock endpoint [mock:direct:foo]
Copy to Clipboard Toggle word wrap
注意

模拟端点不使用参数
端点,模拟它们的参数会被剥离。例如,端点 log:foo?showAll=true 将模拟到以下端点 模拟:log:foo。请注意,参数已被删除。

它还可能仅使用模式模拟某些端点。例如,模拟您 执行 的所有日志端点,如下所示:

提供只使用模式的模拟日志端点 的建议

    @Test
    public void testAdvisedMockEndpointsWithPattern() throws Exception {
        // advice the start route using the inlined AdviceWith lambda style route builder
        // which has extended capabilities than the regular route builder
        AdviceWith.adviceWith(context, "start", a ->
        // mock only log endpoints
        a.mockEndpoints("log*"));

        // now we can refer to log:foo as a mock and set our expectations
        getMockEndpoint("mock:log:foo").expectedBodiesReceived("Bye World");

        getMockEndpoint("mock:result").expectedBodiesReceived("Bye World");

        template.sendBody("direct:start", "Hello World");

        assertMockEndpointsSatisfied();

        // additional test to ensure correct endpoints in registry
        assertNotNull(context.hasEndpoint("direct:start"));
        assertNotNull(context.hasEndpoint("direct:foo"));
        assertNotNull(context.hasEndpoint("log:foo"));
        assertNotNull(context.hasEndpoint("mock:result"));
        // only the log:foo endpoint was mocked
        assertNotNull(context.hasEndpoint("mock:log:foo"));
        assertNull(context.hasEndpoint("mock:direct:start"));
        assertNull(context.hasEndpoint("mock:direct:foo"));
    }
Copy to Clipboard Toggle word wrap

支持的模式可以是通配符或正则表达式。有关此问题的详细信息,请参阅 Intercept,及其与 Camel 使用的相同匹配功能。

注意

模拟端点时会导致在消息到达模拟时复制消息。
这意味着 Camel 将使用更多内存。当您在很多消息中发送时,可能不适用。

返回顶部
Red Hat logoGithubredditYoutubeTwitter

学习

尝试、购买和销售

社区

关于红帽文档

通过我们的产品和服务,以及可以信赖的内容,帮助红帽用户创新并实现他们的目标。 了解我们当前的更新.

让开源更具包容性

红帽致力于替换我们的代码、文档和 Web 属性中存在问题的语言。欲了解更多详情,请参阅红帽博客.

關於紅帽

我们提供强化的解决方案,使企业能够更轻松地跨平台和环境(从核心数据中心到网络边缘)工作。

Theme

© 2025 Red Hat