'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 failedjava.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); } }
Solution 1:[1]
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.
Solution 2:[2]
This happens when you register 2 listeners with same id.
Most likely you had a class with listener defined and then you manually created another bean (possibly mocked) of the same class.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Artem Bilan |
| Solution 2 | hrabrica |
