我正在尝试为rabbitMq监听器编写一个测试用例。我尝试使用spring-rabbit-test并在运行测试时遇到以下错误:
启动ApplicationContext时出错。显示条件报告 在启用“debug”的情况下重新运行应用程序。 2018-03-06 17:10:50.113 ERROR 14239 --- [main] o.s.boot.SpringApplication
:应用程序运行失败java.lang.IllegalStateException:另一个端点已经存在 注册了id'response_queue' 在org.springframework.util.Assert.state(Assert.java:73)〜[spring-core-5.0.4.RELEASE.jar:5.0.4.RELEASE] 在org.springframework.amqp.rabbit.listener.RabbitListenerEndpointRegistry.registerListenerContainer(RabbitListenerEndpointRegistry.java:151) 〜[弹簧兔2.0.2.RELEASE.jar:2.0.2.RELEASE]
我正在关注[https://docs.spring.io/spring-amqp/reference/htmlsingle/#testing],在他们分享的示例中,理想情况下听众没有@Component作为组件。
现在我的测试类也试图在上面提到的错误中获得监听器结果。
有人可以帮助我吗?
测试配置
@Configuration @RabbitListenerTest public class RabbitMQTestConfig { @Autowired MyListener myListener; }
测试
@ActiveProfiles("test") @RunWith(SpringRunner.class) @SpringBootTest public class RabbitMQTest { @Rule public BrokerRunning brokerRunning = BrokerRunning.isRunning(); @Autowired private RabbitListenerTestHarness harness; @Test public void testMyListener() { }
}
监听
@Component public class MyListener { @Autowired MyService myService; @RabbitListener(id = "response_queue", queues = "response") public void processOrder(SomeResponse someResponse) { myService.process(someResponse); } }
答案 0 :(得分:1)
是的......您需要与我们分享您的项目。这是一个简单的变体,让我们重现并发挥作用来确定原因。
现在我无法使用非常简单的Spring Boot应用程序重现它:
@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;
}
}
似乎对我来说,我没有错过任何一点如何配置这些。