0

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?

4

1 回答 1

1

弄清楚了:

像这样的东西:

Boolean messageWaiting = true;
while (messageWaiting) {
    messageWaiting =
         jmsTemplate.browse(myQueue,
                           (session, browser) -> browser.getEnumeration().hasMoreElements());
}

有了这个而不是线程睡眠,它似乎等待足够长的时间来消耗消息,但不会更长。

于 2016-09-13T10:19:19.990 回答