单元测试中的 JMS 依赖关系
我有一个给定的测试,我想在 JUnit 中运行。它依赖于代码使用 JMS 调用的复杂服务,因此在运行 JUnit 测试时它将无法访问该服务。那么,鉴于我需要调用此服务,那么最好的方法是什么来消除此服务,以便在运行 JUnit 测试时调用时仅返回硬编码响应?
现在它使用 JNDI 来查找队列,并且现在使用 easymock 工作得很好,这样 spring 初始化就不会出现问题。但它现在还需要从存根服务的回复队列中获取响应(非常重要)。
I have a given test which I want to run in JUnit. It has a dependency on a complex service that the code calls using JMS, so while running the JUnit test it will not have access to this. So, given the fact that I need to call this service, what is the best way to stub out this service so that it just returns a hard-coded response when called while the JUnit test is ran?
Right now it uses JNDI to lookup the queue and that works fine now using easymock so that spring initializes without problems. But it also needs to get a response on its reply queue from the stub service now (very important).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您是否考虑过在单元测试中使用嵌入式 ActiveMQ?
http://activemq.apache.org/how-to-unit -test-jms-code.html
Have you considered using an embedded ActiveMQ within your unit tests?
http://activemq.apache.org/how-to-unit-test-jms-code.html
响应应该如何传递到回复队列?我假设这是通过在某处调用特定的回调方法来发生的。谁有权访问该方法?
如果回复队列被传递到存根服务,您可以通过 EasyMock 捕获它,然后直接调用它的方法。 EasyMock 文档(搜索“capture”)中非常简要地讨论了执行此操作的方法。一个简单的例子:
How is the response supposed to be passed onto the reply queue? I assume it happens by calling a specific callback method somewhere. Who has access to that method?
If the reply queue is passed to the stub service, you can capture it via EasyMock and then invoke methods on it directly. The way to do this is very briefly discussed in the EasyMock documentation (search for "capture"). A simple example: