Skip to content

RabbitListenerTestHarness does not support repeatable @RabbitListener annotations #1494

Closed
@garyrussell

Description

@garyrussell

Discussed in #1493

Originally posted by ashvydkyi August 22, 2022
Hi!
I've created SpringBootTest to test Rabbit Listener. While using the @RabbitListenerTest annotation, I want to use the capture advice.

import com.rabbitmq.client.Channel;
import org.junit.jupiter.api.Test;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.rabbit.config.SimpleRabbitListenerContainerFactory;
import org.springframework.amqp.rabbit.connection.Connection;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.rabbit.test.RabbitListenerTest;
import org.springframework.amqp.rabbit.test.RabbitListenerTestHarness;
import org.springframework.amqp.rabbit.test.TestRabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.io.IOException;
import java.util.concurrent.TimeUnit;

import static org.mockito.ArgumentMatchers.anyBoolean;
import static org.mockito.BDDMockito.given;
import static org.mockito.BDDMockito.willReturn;
import static org.mockito.Mockito.mock;

@SpringBootTest(classes = StringMessageListenerTest.TestConfiguration.class)
@RabbitListenerTest(spy = false, capture = true)
public class StringMessageListenerTest {

    @Configuration
    public static class TestConfiguration {
        @Bean
        public TestRabbitTemplate testRabbitTemplate() throws IOException {
            return new TestRabbitTemplate(mockConnectionFactory());
        }

        @Bean
        public ConnectionFactory mockConnectionFactory() throws IOException {
            ConnectionFactory factory = mock(ConnectionFactory.class);
            Connection connection = mock(Connection.class);
            Channel channel = mock(Channel.class);
            willReturn(connection).given(factory).createConnection();
            willReturn(channel).given(connection).createChannel(anyBoolean());
            given(channel.isOpen()).willReturn(true);
            return factory;
        }

        @Bean
        public SimpleRabbitListenerContainerFactory rabbitListenerContainerFactory() throws IOException {
            SimpleRabbitListenerContainerFactory factory = new SimpleRabbitListenerContainerFactory();
            factory.setConnectionFactory(mockConnectionFactory());
            return factory;
        }

        @RabbitListener(id = "string1", queues = "queue1")
        public void onMessage(String string) {
        }

        @RabbitListener(id = "string2", queues = "queue2")
        @RabbitListener(id = "string3", queues = "queue3")
        public void onRepeatedMessage(String string) {
        }
    }

    @Autowired
    private RabbitListenerTestHarness rabbitListenerTestHarness;
    @Autowired
    private RabbitTemplate rabbitTemplate;

    @Test
    void whenRabbitListenerAnnotationUsed_shouldHandleStringSuccessfully() throws Exception {
        this.rabbitTemplate.convertAndSend("queue1", "string");

        var invocationData = rabbitListenerTestHarness.getNextInvocationDataFor("string1", 5, TimeUnit.SECONDS);
        var message = (String) invocationData.getArguments()[0];

        assert message.equals("string");
    }

    @Test
    void whenRepeatedRabbitListenerAnnotationUsed_shouldHandleStringSuccessfully() throws Exception {
        this.rabbitTemplate.convertAndSend("queue2", "string");

        var invocationData = rabbitListenerTestHarness.getNextInvocationDataFor("string2", 5, TimeUnit.SECONDS);
        var message = (String) invocationData.getArguments()[0];

        assert message.equals("string");
    }
}

SpringBootParentVersion

   <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.7.3</version>
    </parent>

with the latest (based on maven central)

  <groupId>org.springframework.amqp</groupId>
  <artifactId>spring-rabbit-test</artifactId>
  <version>2.4.6</version>

JavaVersion 17

It seems that like org.springframework.amqp.rabbit.test.RabbitListenerTestHarness.CaptureAdvice doesn't support repeatable annotation processing. Am I missing something?
Thanks in advance.

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions