Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to construct a javax.jms.TextMessage in unit tests?

What is the best way to construct a TextMessage from a String in a unit test?

I see only Session#createTextMessage(String), but that requires a session which requires some setup.

The alternative is just to mock the TextMessage, but I thought it is a good practice to avoid mocking data objects.

like image 462
Philipp Claßen Avatar asked Jan 18 '16 10:01

Philipp Claßen


1 Answers

Try to use the constructor for your broker's implementation of TextMessage.

For example, if you use ActiveMQ, you could create a new ActiveMQTextMessage object via the default constructor, and use the setText setter to set the payload.

See http://activemq.apache.org/maven/apidocs/org/apache/activemq/command/ActiveMQTextMessage.html.

If the broker library is not in the classpath of your unit tests, nor you intend to include it, you'll have no other option than to mock it, given that the JMS API doesn't come with a default implementation.

Using a mock in this situation is perfectly valid and reasonable.

like image 195
raulk Avatar answered Oct 17 '22 11:10

raulk