Search code examples
springtestingspring-integration

Spring integration: testing poller dependent logic


I wonder how could I write spring tests to assert logic chain which is triggered by 'SourcePollingChannelAdapter'.

What comes to my mind:

  • use Thread.sleep() which is really bad idea for tests
  • Have another test version of spring context where I will replace all pollable channels with direct ones. This requires much work.

Are there any common ways to force trigger poller within test?


Solution

  • Typically we use QueueChannel in our tests and wait for the messages via its receive(10000) method. This way, independently of the source of data, our test method thread is blocked until data has arrived.

    The SourcePollingChannelAdapter is triggered by the TaskScheduler, therefore the whole flow logic is done within a separate thread from the test method. I mean that your idea about replacing channels won't help. The Thread.sleep() might have value, but QueueChannel.receive(10000) is much reliable because we really maximum wait only for those 10 seconds.

    Another way to block test-case comes from the standard CountDownLatch, which you would countDown() somewhere in the flow and wait for it in the test method.

    There is some other way to test: have some loop with short sleep period in between iteration and check some condition to exit and verify. That may be useful in case of poller and database in the end. So, we would perform SELECT in that loop until desired state.

    You can find some additional info in the Reference Manual.