Reputation: 12180
I have a suite of integration tests, which creates a full Spring context for my application, mocks out the end points towards other application and runs tests, for instance calling a REST-service and then check the result in the database, that other web/REST-services has been called, and so on.
On of the cases I would like to test is consumation of JMS messages, using Spring Integration. For these test cases, I create an in-memory ActiveMQ broker, place a message on it, and check that the consumation of the message has the desired the result. This approach has a problem that I can't find a good solution for.
Since the message processing happens in thread separate from my test exection, I have to wait for some time before verifying the behaviour. Right now I do this by Thread.sleep
, but I'm not really happy about it. First, it adds build time, and secondly it is prone to random errors if set too low. This results in each of these tests sleeping for 2 seconds just to be sure.
Is there a way I can control whether a message has been processed? A way to check the ActiveMQ broker through JmsTemplate perhaps?
Upvotes: 0
Views: 1349
Reputation: 12180
Figured it out:
Something like this:
Boolean messageWaiting = true;
while (messageWaiting) {
messageWaiting =
jmsTemplate.browse(myQueue,
(session, browser) -> browser.getEnumeration().hasMoreElements());
}
With this instead of thread sleep it seems to wait long enough for the message to be consumed, but not longer.
Upvotes: 1