使用 MockEndpoints 测试 Camel [英] Testing Camel with MockEndpoints

查看:32
本文介绍了使用 MockEndpoints 测试 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 为每个组件提供单元测试.我一直在翻阅 Camel 网站上的 javadocs 和几个例子,但我很难把所有的点联系起来.

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,有几种方法可以做到这一点.

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 在运行之前动态更改路由(添加日志/模拟端点等)
  • 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");

这篇关于使用 MockEndpoints 测试 Camel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆