Camel - 向模拟输入发送消息

发布于 2024-11-28 22:33:45 字数 1341 浏览 0 评论 0原文

如何用模拟替换重量级输入端点并向其发送消息?

假设我有以下路线。

from("spring-ws:rootqname:{http://myNameSpace}SomeRequest?endpointMapping=#endpointMapping").
    processor(new VeryComplicatedProcessor()).
    to(etc).
    to(etc)...

换句话说,一个 Spring Web 服务可以进行大量的处理。

测试此路由的一种方法是在网络服务器中启动网络服务并向其发送消息,但这比我想要的开销要大得多。

相反,我希望如果我 模拟我的所有端点,我应该能够这样做:

public class RouteTest extends CamelSpringTestSupport {

    @Override
    public String isMockEndpoints() {
        return "*";   // mock everything
    }

    @Test
    public void simpleDirectTest() throws Exception {

        String realURI = "spring-ws:rootqname:{http://myNameSpace}SomeRequest?endpointMapping=#endpointMapping";
        String mockedURI = "mock:" + realURI;
        String msg = buildSomeSoapMsg();

        Object out = template.requestBody(mockedURI, msg);
        log.debug(out.toString());
    }
...
}

...并运行整个路线并将响应返回给我。相反,我只是收到直接发回给我的消息,并且路线尚未运行。

我对模拟的理解不正确吗?如果是这样,是否有其他方法可以实现这一目标?我真的很想避免必须实际启动网络服务来测试这些路由。

谢谢大家。

编辑 - 看起来我可以使用 AdviceWith 来解决这个问题,但 AdviceWith 似乎只适用于输出,而不适用于上面的情况的输入。

How do I replace a heavy-weight input endpoint with a mock and send a message to it?

Say I have the following route.

from("spring-ws:rootqname:{http://myNameSpace}SomeRequest?endpointMapping=#endpointMapping").
    processor(new VeryComplicatedProcessor()).
    to(etc).
    to(etc)...

In other words, a spring web service that does a whole lotta processing.

One way to test this route is to start up the webservice in a webserver and send it a message, but that's a lot more overhead than I'd like to incur.

Instead, I'd expect that if I mock all of my endpoints, I should be able to do this:

public class RouteTest extends CamelSpringTestSupport {

    @Override
    public String isMockEndpoints() {
        return "*";   // mock everything
    }

    @Test
    public void simpleDirectTest() throws Exception {

        String realURI = "spring-ws:rootqname:{http://myNameSpace}SomeRequest?endpointMapping=#endpointMapping";
        String mockedURI = "mock:" + realURI;
        String msg = buildSomeSoapMsg();

        Object out = template.requestBody(mockedURI, msg);
        log.debug(out.toString());
    }
...
}

... and have the entire route run and return the response to me. Instead though, I just get the message sent right back to me, and the route hasn't been run.

Is my understanding of mocks incorrect? If so is there an alternative way to achieve this? I'd really like to avoid having to actually start the webservice to test these routes.

Thanks all.

EDIT - It looked for a minute like I could use AdviceWith to solve this problem, but AdviceWith only seems to work with outputs, and not inputs as is the case above.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

青瓷清茶倾城歌 2024-12-05 22:33:45

在 Apache Camel 2.9 中,通知中有一个新的 ReplaceFrom 方法,它允许您在使用其他端点进行单元测试之前替换使用者。例如,您可以将 spring-ws 替换为直接端点。然后向单元测试中的直接端点发送消息。与本在上面的回答中发布的内容在同一行。详细信息请参见:http://camel.apache.org/advicewith.html

In Apache Camel 2.9 there is a new replaceFrom method in the advice with, which allows you to replace a consumer before unit testing with some other endpoint. For example you could replace the spring-ws with a direct endpoint. And then send a message to that direct endpoint in your unit test. On the same line as what Ben posted above in his answer. See details at: http://camel.apache.org/advicewith.html

娜些时光,永不杰束 2024-12-05 22:33:45

你有几个选择...如果你只是想测试路由的其余部分(从 VeryComplicatedProcessor 开始),那么你可以拆分路由,以便你可以使用直接测试它,等等...

from("spring-ws:rootqname:  {http://myNameSpace}SomeRequest?endpointMapping=#endpointMapping").
.to("direct:processRequest");

from("direct:processRequest").
processor(new VeryComplicatedProcessor()).
to(etc).
to(etc)...

另一个选择是只需在单元测试中启动 Web 服务并使用 HTTP 客户端 API 来模拟对真实路由的请求。请参阅 camel-spring-ws 示例

最后,如果您确实需要替换测试的端点,请使用 properties 覆盖端点定义或 adviceWith拦截 策略为您提供甚至更灵活(尽管一开始可能会令人困惑)...

you have a few options...if you are just trying to test the rest of the route (from VeryComplicatedProcessor on), then you can split up the route so that you can test it using direct, etc...

from("spring-ws:rootqname:  {http://myNameSpace}SomeRequest?endpointMapping=#endpointMapping").
.to("direct:processRequest");

from("direct:processRequest").
processor(new VeryComplicatedProcessor()).
to(etc).
to(etc)...

Another option is to just standup the webservice in the unit test and use HTTP client APIs to simulate requests to the real route. See the camel-spring-ws examples

Finally, if you really need to replace an endpoint for a test, then use properties to override the endpoint definition or adviceWith or intercept strategies to give you even more flexibility (though can be confusing at first)...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文