Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using spring-rabbit-test for Junit

I am trying to write a test case for rabbitMq listener. I tried using spring-rabbit-test and got the below error while running tests:

Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled. 2018-03-06 17:10:50.113 ERROR 14239 --- [ main] o.s.boot.SpringApplication
: Application run failed

java.lang.IllegalStateException: Another endpoint is already registered with id 'response_queue' at org.springframework.util.Assert.state(Assert.java:73) ~[spring-core-5.0.4.RELEASE.jar:5.0.4.RELEASE] at org.springframework.amqp.rabbit.listener.RabbitListenerEndpointRegistry.registerListenerContainer(RabbitListenerEndpointRegistry.java:151) ~[spring-rabbit-2.0.2.RELEASE.jar:2.0.2.RELEASE]

I was following [https://docs.spring.io/spring-amqp/reference/htmlsingle/#testing] and in the example they have shared it does not have an @Component for the listeners in ideal case that will be a component.

Now my test class also tries to get the listener resultin in the error mentioned above.

Can someone help me?

Test configuration

@Configuration    
@RabbitListenerTest
public class RabbitMQTestConfig {
    @Autowired
    MyListener myListener;
}

Test

@ActiveProfiles("test")
@RunWith(SpringRunner.class)
@SpringBootTest
public class RabbitMQTest {

    @Rule
    public BrokerRunning brokerRunning = BrokerRunning.isRunning();

    @Autowired
    private RabbitListenerTestHarness harness;

    @Test
    public void testMyListener() {

    }

}

Listener

@Component
public class MyListener {

@Autowired
MyService myService;


@RabbitListener(id = "response_queue", queues = "response")
    public void processOrder(SomeResponse someResponse) {
        myService.process(someResponse);
    }
}
like image 242
Kency Kurian Avatar asked Oct 20 '25 13:10

Kency Kurian


1 Answers

Yeah... You will need to share your project with us. It's simple variant to let us to reproduce and play to determine the reason.

Right now I can't reproduce it with very simple Spring Boot app:

@SpringBootApplication
public class So49129095Application {

    public static void main(String[] args) {
        SpringApplication.run(So49129095Application.class, args);
    }
}

@Component
public class MyListener {

    @RabbitListener(id = "response_queue", queuesToDeclare = @Queue("response"))
    public void processOrder(Object payload) {

    }

}

@RunWith(SpringRunner.class)
@SpringBootTest(classes = {So49129095Application.class, So49129095ApplicationTests.RabbitMQTestConfig.class})
public class So49129095ApplicationTests {

    @Rule
    public BrokerRunning brokerRunning = BrokerRunning.isRunning();

    @Autowired
    private RabbitListenerTestHarness harness;

    @Test
    public void testMyListener() {

    }

    @Configuration
    @RabbitListenerTest
    public static class RabbitMQTestConfig {

        @Autowired
        MyListener myListener;

    }

}

Seems for me I didn't miss any of your points how to configure that all together.

like image 61
Artem Bilan Avatar answered Oct 22 '25 03:10

Artem Bilan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!