测试骆驼与MockEndpoints [英] Testing Camel with MockEndpoints

查看:311
本文介绍了测试骆驼与MockEndpoints的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一系列的流水线组件全部通过ActiveMQ消息队列通信。每个组件用骆驼来对待每一个这些队列作为终点的。每个组件使用了相同的基本模式:

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:

每个组件使用消息关闭输入队列,处理信息(S),然后放置在1+出站/输出队列的消息。那么输出队列成为链中的下一个组件输入队列。 pretty基础。

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.

我现在想挽起衣袖,并使用为每个组件提供单元测试由骆驼的测试API提供的 MockEndpoints 。我一直在浇过的javadoc和骆驼网站上的几个例子,但我有连接所有的点难度。

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:


  • 如何连接补偿 inputQueue outputQueue MockEndpoints?

  • 对于每个 MockEndpoint ,我该如何设置的期望,使 assertIsSatisfied()检查邮件是$特定队列里面p $ psent,或一个特定的队列包含消息?

  • 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组件,黑盒独立于任何背景的骆驼/路由测试他们关注业务逻辑。

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 建立交易所验证前pressions(有点复杂,让你的头左右)

  • 使用 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)

我preFER 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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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