使用 MockEndpoints 测试 Camel
我有一系列“管道”组件,它们都通过 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 theinputQueue
andoutputQueue
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:
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 theinputQueue
andoutputQueue
MockEndpoints? - For each
MockEndpoint
, how do I set up expectations so thatassertIsSatisfied()
checks that a message is present inside a particular queue, or that a particular queue contains messages?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
亚当,有几种方法可以做到这一点。
对于 POJO 组件,将它们与任何 Camel 上下文/路由分开进行黑盒测试,以专注于业务逻辑。
如果您想要对路由进行端到端测试,请考虑使用其中一种方法来验证路由中的每个步骤是否得到满足。
我更喜欢 AdviceWith 因为它非常灵活并利用熟悉的模拟端点。有关完整示例,请参阅 此单元测试
简而言之,您将创建一个单元测试以将 MockEndpoints 注入您的路线,然后像往常一样对其进行验证...
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.
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...