Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RabbitListenerTestHarness does not support repeatable @RabbitListener annotations #1494

Closed
garyrussell opened this issue Aug 22, 2022 Discussed in #1493 · 0 comments · Fixed by #1495
Closed

RabbitListenerTestHarness does not support repeatable @RabbitListener annotations #1494

garyrussell opened this issue Aug 22, 2022 Discussed in #1493 · 0 comments · Fixed by #1495

Comments

@garyrussell
Copy link
Contributor

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.

@garyrussell garyrussell self-assigned this Aug 22, 2022
garyrussell added a commit to garyrussell/spring-amqp that referenced this issue Aug 22, 2022
Resolves spring-projects#1494

Capture mode failed to capture arguments/result/exception if multiple
`@RabbitListener` annotations present.

**cherry-pick to 2.4.x**
artembilan pushed a commit that referenced this issue Aug 29, 2022
Resolves #1494

Capture mode failed to capture arguments/result/exception if multiple
`@RabbitListener` annotations present.

**cherry-pick to 2.4.x**
artembilan pushed a commit that referenced this issue Aug 29, 2022
Resolves #1494

Capture mode failed to capture arguments/result/exception if multiple
`@RabbitListener` annotations present.

**cherry-pick to 2.4.x**
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant