Java 模拟或模拟消息队列 (JMS)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3970567/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Mock or simulate Message Queue (JMS)
提问by sergionni
There is a message(text), which format and content i definitely know.
For now,class in Java,that parses and reads this message from file,is implemented.
有一条消息(文本),我肯定知道哪种格式和内容。
现在,实现了从文件中解析和读取此消息的 Java 类。
In real world, this message will come from Message Queue.
在现实世界中,此消息将来自 Message Queue。
For now I should simulate, mock or generate Message Queue on my local PC for testing purposes.
现在,我应该在本地 PC 上模拟、模拟或生成消息队列以进行测试。
Java spec(java jms):
Java规范(java jms):
JMS provider: A messaging system that implements the JMS specification.
JMS clients: Java applications that send and receive messages.
Messages: Objects that are used to communicate information between JMS clients.
Concerning this specification, i need JMS provider.
关于这个规范,我需要JMS provider。
JMS client-it's my class that reads message.
Messageitself i know.
JMS 客户端- 这是我的类读取消息。
消息本身我知道。
So the question is how to start message queue?
How can i simulate it programmaticaly from Java code? Can i mock it somehow?
那么问题来了,如何启动消息队列呢?
我如何从 Java 代码以编程方式模拟它?我可以以某种方式嘲笑它吗?
Thanks.
谢谢。
采纳答案by jcalvert
If you use Spring Integration, you can do this pretty easily. It has a very basic, abstract "Channel" implementation. You can create and test your producers and consumers, and when you're ready to move a step further, you just specify a JMS adapter on top of your Channel.
如果您使用Spring Integration,您可以很容易地做到这一点。它有一个非常基本的、抽象的“通道”实现。您可以创建和测试您的生产者和消费者,当您准备更进一步时,您只需在您的频道之上指定一个 JMS 适配器。
回答by Eugene Kuleshov
Generally it is a bad practice to mock or simulate an external system, such as JMS. A better idea would be to abstract your logic into a standalone bean, implement a delegation layer that would bridge JMS with your bean. With such design you can test your bean in isolation from JMS and then have system test that would test the whole integration with real JMS system.
通常,模拟或模拟外部系统(例如 JMS)是一种不好的做法。更好的想法是将您的逻辑抽象为一个独立的 bean,实现一个将 JMS 与您的 bean 桥接的委托层。通过这样的设计,您可以独立于 JMS 测试您的 bean,然后进行系统测试,以测试与真实 JMS 系统的整个集成。
As for in-process JMS you can look at SomnifugiJMS.
至于进程内 JMS,您可以查看SomnifugiJMS。
回答by AlexR
Generally I agree with Eugene Kuleshov. But if you still need such mocking I'd suggest you to use BlckingQueue from java.util.concurent package. I think it is not a big problem to wrap it with javax.jms.Queue interface. BTW it is a good idea for some kind of open-source project.
总的来说,我同意 Eugene Kuleshov 的观点。但是如果你仍然需要这样的模拟,我建议你使用 java.util.concurent 包中的 BlckingQueue 。我觉得用 javax.jms.Queue 接口包装起来问题不大。顺便说一句,对于某种开源项目来说,这是一个好主意。
回答by Wojtek
To test an application in isolation when the real production JMS provider is not available you can use one of:
要在真正的生产 JMS 提供程序不可用时单独测试应用程序,您可以使用以下方法之一:
JMS mock:
When testing your applications you can simulate the non-existing dependencies using test doubles. You can use a JMS mock which will simulate the behaviour of a real JMS provider. API simulation toolswill allow you to create JMS mocks (just choose a tool that supports JMS, for example Traffic Parrot). Using a JMS mock will allow you for a high level of flexibility during testing. You will be able to test typical production-like test scenarios but also hypothetical situations by setting up your mock to return almost any type of message. You will also be able to simulate different types of errors, which is often hard to do with real JMS providers. Have a look at this introduction video to JMS service virtualization for ActiveMq(service virtualization is a different name for a mock) or this one for IBM MQ. Note, these videos are from Traffic Parrot, but the principle described there will apply to any tool you choose.JMS provider test instance:
You can run a JMS provider on your laptop or in one of your test environments and connect your application to it instead of the production provider. When you use open source providers in production like ActiveMQ or RabbitMQ, it should be easy to run one of them on your laptop as well because they are lightweight and free. For IBM Websphere MQ, you can use the free IBM MQ for Developers.JMS class mock:
You can use Mockitoin unit tests to mock interactions with JMS classes. This solution comes with all the trade-offs of unit testing. For more information on those see testing pyramid.
JMS 模拟:
在测试应用程序时,您可以使用test doubles模拟不存在的依赖项。您可以使用 JMS 模拟来模拟真实 JMS 提供程序的行为。API 模拟工具将允许您创建 JMS模拟(只需选择支持 JMS 的工具,例如Traffic Parrot)。使用 JMS 模拟将使您在测试期间具有高度的灵活性。通过设置模拟以返回几乎任何类型的消息,您将能够测试典型的类似生产的测试场景以及假设的情况。您还可以模拟不同类型的错误,这对于真正的 JMS 提供者来说通常是很难做到的。看一下此介绍视频用于 ActiveMq(服务虚拟化是模拟的不同名称)或IBM MQ 的JMS 服务虚拟化。请注意,这些视频来自 Traffic Parrot,但其中描述的原则适用于您选择的任何工具。JMS 提供程序测试实例:
您可以在您的笔记本电脑或您的测试环境之一中运行 JMS 提供程序,并将您的应用程序连接到它而不是生产提供程序。当您在生产中使用 ActiveMQ 或 RabbitMQ 等开源提供程序时,在您的笔记本电脑上运行其中之一应该也很容易,因为它们是轻量级且免费的。对于 IBM Websphere MQ,您可以使用免费的IBM MQ for Developers。JMS 类模拟:
您可以在单元测试中使用Mockito来模拟与 JMS 类的交互。该解决方案伴随着单元测试的所有权衡。有关这些的更多信息,请参阅测试金字塔。
If you would like to black box test your application, use one of the solutions I have described above.
如果您想对您的应用程序进行黑盒测试,请使用我上面描述的解决方案之一。