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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With