使用 MockEndpoints 测试骆驼

2022-01-19 00:00:00 unit-testing messaging java apache-camel

我有一系列流水线"组件,它们都通过 ActiveMQ 消息队列进行通信.每个组件都使用 Camel 将这些队列中的每一个视为一个端点.每个组件都使用相同的基本模式:

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:

每个组件使用输入队列中的消息,处理消息,然后将 1+ 消息放在出站/输出队列中.然后输出"队列成为链中下一个组件的输入"队列.很基本.

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.

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

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:

  • 测试是否有消息在特定的输入"队列中等待
  • 下拉这些消息并处理它们
  • 将新消息推送到输出"队列并验证它们是否到达那里

我相信我需要像这样为每个队列创建 MockEndpoints:

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;

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

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:

  • 如何将 comp 连接到 inputQueueoutputQueue MockEndpoints?
  • 对于每个 MockEndpoint,我如何设置期望,以便 assertIsSatisfied() 检查消息是否存在于特定队列中,或者特定队列是否包含消息?
  • 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?

推荐答案

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

Adam, there are several ways to do this.

对于 POJO 组件,将它们与任何 Camel 上下文/路由分开进行黑盒测试以获取焦点关于业务逻辑.

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.

  • 使用 NotifyBuilder 构建 Exchange 验证表达式(理解起来有点复杂)
  • 使用 AdviceWith 在运行前动态更改路由(添加 Log/Mock 端点等)
  • 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)

我更喜欢 AdviceWith,因为它非常灵活并且利用了熟悉的 MockEndpoints.有关这方面的完整示例,请参阅 这个单元测试

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

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

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");

相关文章