springrabbitmqspring-testspring-rabbit

Spring rabbitmq testing - RabbitListenerTestHarness doesn't find any listener


I'm trying to write some tests for a spring rabbitmq application.

From documentation, I found the example using RabbitListenerTestHarness.

I created a Test in spring that looks like:

@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = MyMain.class)
public class MyClassTest {

    @Test
    public void myTest(){
        RabbitProperties.Listener listener = harness.getSpy("test_consumer2");
    }
}

In the source, I have a bean that creates the configuration to rabbitmq which has the annotation @RabbitListenerTest(capture = true, spy = true).

Also, in the source I have the following consumer:

@Component

public class TestConsumerStub {

@RabbitListener(
        id = "test_consumer2",
        queues = "my_queue"
)
public void testHandler(){

}

For some reason, the harness (RabbitListenerTestHarness harness) object from the tests, is not loaded in the context that consumer. The following variable is null: RabbitProperties.Listener listener = harness.getSpy("test_consumer2");

Any idea what did I missed?


Solution

  • After debuging the RabbitListenerTestHarness bean I figured out which was the problem. I was using spring-boot-starter-amqp for spring rabbit and for spring-rabbit-test I was writing explicitly the version, which was not matching the version of spring-amqp. I updated spring-boot-starter-parent version to 1.4.0 which was installing spring-ampq version 1.6.1 which is the same version of my spring-rabbit-test.