Jayadeep23
Jayadeep23

Reputation: 13

RabbitListenerTestHarness injects actual objects in listeners

Scenario: Junit for a microservice which listens to a queue and posts to an exchange in rabbitMQ after data extraction.

Issue:

RabbitListenerTestHarness is creating mock object for the Rabbit Listener class alone, Actual objects are being instantiated for Listeners Autowired components

I couldnt find a way to manually inject mock beans into the listener. This causes Junit to post the test messages to the actual queues configured in the microservice during Junit Execution.

Workaround: The only way I could use the rabbit-test project is to configure test exchange for posting the messages during Junit execution.

Query: I wanted to understand, if there is any way better way of writing Junit for a Rabbit Listener. Also i wanted to understand if there is a way to maually inject mock objects to the Rabbit Listeners autowired components.

Sample code Snippet:

Rabbit Listener Class

@RabbitListener(id = "id", bindings = @QueueBinding(value = @Queue(value = "sampleQueue", durable = "true", autoDelete = "false"),key = "sampleRoutingKey", exchange = @Exchange(value = "sampleExchange", durable = "true", ignoreDeclarationExceptions = "true", type = EXCHANGE_TYPE)))
public void getMessageFromQueue(@Payload EventModel event) throws ListenerExecutionFailedException, JAXBException {
    dataExporterService.exportDataAndPostToRabbit(event);
}

Service class

@Autowired
DataExtractorRepository dataExtractorRepository;
@Autowired
DataPublihserRepository dataPublisherRepo;
public void exportDataAndPostToRabbit(EventModel event) throws JAXBException {
        dataPublisherRepo.sendMessageToExchange(dataExtractorRepository.extractOrderData(event), exchangeName, routingKeyValue);
}

DataPublihserRepository has rabbitTemplate internally Autowired. DataExtractorRepository connects to DB internally for retriving the message.

Test class

@Autowired
private RabbitListenerTestHarness harness;

@Autowired
private RabbitTemplate rabbitTemplate;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
DataExporterController = this.harness.getSpy("id");
}

@Test
public void shouldReceiveMessage() throws Exception {
LatchCountDownAndCallRealMethodAnswer answer = new LatchCountDownAndCallRealMethodAnswer(1);
doAnswer(answer).when(DataExporterController).getMessageFromQueue(any(EventModel.class));
rabbitTemplate.convertAndSend("sampleExchange", "sampleRoutingKey", createMessage());
assertTrue(answer.getLatch().await(10, TimeUnit.SECONDS));
verify(DataExporterController, times(1)).getMessageFromQueue(any(OrderEventsModel.class));
verify(orderDataExporterController, times(1)).getMessageFromQueue(any(OrderEventsModel.class));
}

 private Message createMessage() {
        String inputObject = "{\"id\":12345}";
        MessageProperties props = MessagePropertiesBuilder.newInstance().setContentType(MessageProperties.CONTENT_TYPE_JSON).build();
        return new Message(inputObject.getBytes(), props);
}

Upvotes: 1

Views: 1643

Answers (1)

Gary Russell
Gary Russell

Reputation: 174729

The harness is intended as a mechanism to verify that the listener received the data in an integration test. To unit test a listener, invoke its onMessage Method.

For example, using Mockito, given

public class MyListener {

    @Autowired
    private SomeService service;

    @RabbitListener(id = "myListener", queues = "foo")
    public void listen(Foo foo) {
        this.service.process(foo);
    }

}

and

public interface SomeService {

    void process(Foo foo);

}

then

@RunWith(SpringRunner.class)
public class So53136882ApplicationTests {

    @Autowired
    private RabbitListenerEndpointRegistry registry;

    @Autowired
    private SomeService service;

    @Test
    public void test() throws Exception {
        SimpleMessageListenerContainer container = (SimpleMessageListenerContainer) this.registry
                .getListenerContainer("myListener");
        ChannelAwareMessageListener listener = (ChannelAwareMessageListener) container.getMessageListener();
        Message message = MessageBuilder.withBody("{\"bar\":\"baz\"}".getBytes())
                .andProperties(MessagePropertiesBuilder.newInstance()
                        .setContentType("application/json")
                        .build())
                .build();
        listener.onMessage(message, mock(Channel.class));
        verify(this.service).process(new Foo("baz"));
    }

    @Configuration
    @EnableRabbit
    public static class config {

        @Bean
        public ConnectionFactory mockCf() {
            return mock(ConnectionFactory.class);
        }

        @Bean
        public MessageConverter converter() {
            return new Jackson2JsonMessageConverter();
        }

        @Bean
        public SimpleRabbitListenerContainerFactory rabbitListenerContainerFactory() {
            SimpleRabbitListenerContainerFactory factory = new SimpleRabbitListenerContainerFactory();
            factory.setConnectionFactory(mockCf());
            factory.setMessageConverter(converter());
            factory.setAutoStartup(false);
            return factory;
        }

        @Bean
        public MyListener myListener() {
            return new MyListener();
        }

        @Bean
        public SomeService service() {
            return mock(SomeService.class);
        }

    }

}

Notice that the container factory does not start the listener container.

For testing publishing, inject a mock RabbitOperations which is implemented by RabbitTemplate.

For example, given

public class SomeServiceImpl implements SomeService {

    @Autowired
    private RabbitOperations rabbitOperations;

    @Override
    public void process(Foo foo) {
        this.rabbitOperations.convertAndSend(
                "someExchange", "someRoutingKey", new Foo(foo.getBar().toUpperCase()));
    }

}

and

@Bean
public SomeService service() {
    return new SomeServiceImpl();
}

@Bean
public RabbitOperations rabbitTemplate() {
    return mock(RabbitOperations.class);
}

then

@Test
public void test() throws Exception {
    SimpleMessageListenerContainer container = (SimpleMessageListenerContainer) this.registry
            .getListenerContainer("myListener");
    ChannelAwareMessageListener listener = (ChannelAwareMessageListener) container.getMessageListener();
    Message message = MessageBuilder.withBody("{\"bar\":\"baz\"}".getBytes())
            .andProperties(MessagePropertiesBuilder.newInstance()
                    .setContentType("application/json")
                    .build())
            .build();
    listener.onMessage(message, mock(Channel.class));
    verify(this.rabbitTemplate).convertAndSend("someExchange", "someRoutingKey", new Foo("BAZ"));
}

Upvotes: 1

Related Questions