Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mock or simulate Message Queue (JMS)

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.

In real world, this message will come from Message Queue.

For now I should simulate, mock or generate Message Queue on my local PC for testing purposes.

Java spec(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 client-it's my class that reads message.
Message itself i know.

So the question is how to start message queue?
How can i simulate it programmaticaly from Java code? Can i mock it somehow?

Thanks.

like image 668
sergionni Avatar asked Oct 19 '10 16:10

sergionni


People also ask

How do you mock a message in queue?

You can use a JMS mock which will simulate the behaviour of a real JMS provider. API simulation tools will 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.

Is JMS push or pull?

Push delivery is simpler as the JMS provider automatically delivers messages to registered JMS consumers. Developers incorporate the pull or the push delivery mechanism based on the business logic requirements.

What is JMS message queue?

JMS supports two different message delivery models: Point-to-Point (Queue destination): In this model, a message is delivered from a producer to one consumer. The messages are delivered to the destination, which is a queue, and then delivered to one of the consumers registered for the queue.


2 Answers

To test an application in isolation when the real production JMS provider is not available you can use one of:

  1. 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 tools will 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.

  2. 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.

  3. JMS class mock:
    You can use Mockito in 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.

If you would like to black box test your application, use one of the solutions I have described above.

like image 194
Wojtek Avatar answered Oct 17 '22 18:10

Wojtek


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.

like image 31
jcalvert Avatar answered Oct 17 '22 17:10

jcalvert