camel-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Babak Vahdat <>
Subject Re: mock.expectedMessageCount(0) resulting in a false positive
Date Wed, 12 Dec 2012 19:35:32 GMT

Am 12.12.12 16:08 schrieb "Babak Vahdat" unter

>Am 12.12.12 14:23 schrieb "" unter
>>I'm running an integration test using the CamelSpringJUnit4ClassRunner to
>>test a route JMS -> Processor -> SFTP endpoint. In my test, I use a
>>ProducerTemplate to fire a message to the JMS endpoint and mock the SFTP
>>endpoint where I assert sftpMockEndpoint.setExpectedMessageCount(1). This
>>all works fine.
>>I've now written a test where I send an invalid message to the JMS queue.
>>Normally this is caught by the processor which logs the message and stops
>>any further processing. The SFTP endpoint should not receive a message in
>>this case, so I've added:
>>When I run this test and pass in a valid message (i.e. one that reaches
>>SFTP endpoint), I'm still getting a positive result - the SFTP endpoint
>>receives a message AND the setExpectedMessageCount(0) assertion passes.
>If you don't want an explicit Thread.sleep() call inside your code then
>you could make use of the MockEndpoint.setSleepForEmptyTest() API.
>>I believe that because the JMS queue is asynchronous, the assertions are
>>being fired before the message has passed along the route  - is this
>>correct? E.g.:
>>1. set mock expectation sftpMockEndpoint.expectedMessageCount(0);
>>2. fire message to JMS queue. Starts an async process and return control
>>test method
>>3. test method runs sftpMockEndpoint.assertIsSatisfied(); which asserts
>>the SFTP mock has received no messages. Assertion is correct.
>>4. JMS messages continues to be processed and eventually makes it's way
>>the SFTP mock, which results in 1 message being received after the test
>>assertions have been processed.
>>Please could you confirm if this is the case and if there is anything I
>>do to suspend processing of the assertions until the message has gone all
>>the way through the route?

And yes this is indeed the case as consuming a given JMS message happens
inside it's own dedicated thread whereas asserting on the Mockendpoint
occurs inside the main thread. So there's a race condition here one has to
take care of because otherwise (as you've already mentioned) you could end
up with false positive tests.

Also another possible approach (instead of Thread.sleep() or
MockEndpoint.setSleepForEmptyTest()) is to make use of NotifyBuilder where
after firing the exchange into your route through the ProducerTemplate,
using the NotifyBuilder you would wait for the routing of the given
exchange to be completed/failed/done. And then *afterwards* assert on
whatever condition you want on the MockEndpoint. In this way your
assertion will be for sure *after* the routing of the given exchange. For
the semantics of completed/failed/done see:


>> I tried adding a
>>sftpMockEndpoint.whenAnyExchangeReceived(new Processor() { // throw
>>Exception }); to trigger a failure in the test whenever the SFTP mock
>>received an Exchange but unfortunately the Exception gets swallowed up by
>>the test framework.
>>The only solution I have found is to add a Thread.sleep(100); before the
>>test assertions (assertIsSatisfied) are processed, but this is ugly and I
>>don't want arbitrary sleeps within my code.
>>Any help would be greatly appreciated and apologies for the length of
>>post - I wanted to include as much information as possible but not post
>>great swathes of unintelligible code.
>>Thanks again,
>>View this message in context:
>>Sent from the Camel - Users mailing list archive at

View raw message