使用 MockEndpoints 测试 Camel

发布于 2024-12-22 04:20:20 字数 1226 浏览 4 评论 0原文

我有一系列“管道”组件,它们都通过 ActiveMQ 消息队列进行通信。每个组件都使用 Camel 将每个队列视为端点。每个组件都使用相同的基本模式:

基本组件模式

每个组件从输入队列中消耗消息,处理消息( s),然后将 1+ 条消息放入出站/输出队列中。然后,“输出”队列成为链中下一个组件的“输入”队列。非常基本。

我现在正尝试卷起袖子,使用 Camel 的测试 API 提供的 MockEndpoints 为每个组件提供单元测试。我一直在研究 javadoc 和 Camel 网站上的几个示例,但很难将所有点联系起来。

在我看来,对于每个组件,我的单元测试的一部分将要完成以下三件事:

  • 测试以查看是否有消息在等待特定的“输入”队列
  • 拉取这些消息并处理它们
  • 推送将新消息发送到“输出”队列并验证它们是否已到达那里

我相信我需要为每个队列创建 MockEndpoints,如下所示:

@EndpointInject(uri = "mock:inputQueue")
protected MockEndpoint intputQueue;

@EndpointInject(uri = "mock:outputQueue")
protected MockEndpoint outputQueue;

现在,在我的 JUnit 测试方法中,我可以设置期望并与这些端点交互:

@Test
public final void processMethodShouldSendToOutputQueue()
{
    Component comp = new Component();
    comp.process();

    outputQueue.assertIsSatisfied();
}

我只是不明白如何正确连接所有内容:

  • How do I connect comp to the inputQueue and outputQueue MockEndpoints?
  • 对于每个 MockEndpoint,如何设置期望,以便 assertIsSatisfied() 检查特定队列中是否存在消息,或者特定队列是否包含消息?

I've got a series of "pipelined" components that all communicate through ActiveMQ message queues. Each component uses Camel to treat each of these queues as an Endpoint. Each component uses the same basic pattern:

Basic component pattern

Where each component consumes messages off of an input queue, processes the message(s), and then places 1+ messages on an outbound/output queue. The "output" queue then becomes the "input" queue for the next component in the chain. Pretty basic.

I am now trying to roll up my sleeves and provide unit testing for each component using the MockEndpoints provided by Camel's test API. I have been pouring over the javadocs and the few examples on Camel's website, but am having difficulty connecting all the dots.

It seems to me that, for each component, a portion of my unit testing is going to want to accomplish the following three things:

  • Test to see if there are messages waiting on a particular "input" queue
  • Pull those messages down and process them
  • Push new messages to an "output" queue and verify that they made it there

I believe I need to create MockEndpoints for each queue like so:

@EndpointInject(uri = "mock:inputQueue")
protected MockEndpoint intputQueue;

@EndpointInject(uri = "mock:outputQueue")
protected MockEndpoint outputQueue;

So now, in my JUnit test methods, I can set up expectations and interact with these endpoints:

@Test
public final void processMethodShouldSendToOutputQueue()
{
    Component comp = new Component();
    comp.process();

    outputQueue.assertIsSatisfied();
}

I'm just not understanding how to wire everything up correctly:

  • How do I connect comp to the inputQueue and outputQueue MockEndpoints?
  • For each MockEndpoint, how do I set up expectations so that assertIsSatisfied() checks that a message is present inside a particular queue, or that a particular queue contains messages?

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

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

发布评论

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

评论(1

墨小沫ゞ 2024-12-29 04:20:20

亚当,有几种方法可以做到这一点。

对于 POJO 组件,将它们与任何 Camel 上下文/路由分开进行黑盒测试,以专注于业务逻辑。

如果您想要对路由进行端到端测试,请考虑使用其中一种方法来验证路由中的每个步骤是否得到满足。

  • 使用 NotifyBuilder 构建 Exchange 验证表达式(有点复杂,难以理解)
  • 使用 AdviceWith 在运行之前动态更改路由(添加 Log/Mock 端点等)

我更喜欢 AdviceWith 因为它非常灵活并利用熟悉的模拟端点。有关完整示例,请参阅 此单元测试

简而言之,您将创建一个单元测试以将 MockEndpoints 注入您的路线,然后像往常一样对其进行验证...

context.getRouteDefinition("myRouteId").adviceWith(context, new AdviceWithRouteBuilder() {
    @Override
    public void configure() throws Exception {
        // mock all endpoints
        mockEndpoints();
    }
});

getMockEndpoint("mock:direct:start").expectedBodiesReceived("Hello World");

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

Adam, there are several ways to do this.

For POJO components, blackbox test them separately from any Camel context/routing to focus on business logic.

If you want to do end-to-end testing of the routes, consider using one of these approaches to validate that each step in the route is satisfied.

  • use NotifyBuilder to build Exchange validation expressions (somewhat complex to get your head around)
  • use AdviceWith to dynamically change the route before its run (add Log/Mock endpoints, etc)

I prefer AdviceWith because its very flexible and leverages the familiar MockEndpoints. For a complete example of this, see this unit test

In short, you will create a unit test to inject MockEndpoints into your route and then validate against them as usual...

context.getRouteDefinition("myRouteId").adviceWith(context, new AdviceWithRouteBuilder() {
    @Override
    public void configure() throws Exception {
        // mock all endpoints
        mockEndpoints();
    }
});

getMockEndpoint("mock:direct:start").expectedBodiesReceived("Hello World");

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