Java 使用Junit的spring-rabbit测试
我正在尝试为rabbitMq侦听器编写一个测试用例。我尝试使用spring rabbit测试,在运行测试时出现以下错误: 启动ApplicationContext时出错。显示条件报告的步骤 在启用“调试”的情况下重新运行应用程序。2018-03-06 17:10:50.113 错误14239---[main]o.s.boot.SpringApplicationJava 使用Junit的spring-rabbit测试,java,spring,spring-boot,spring-amqp,spring-rabbit,Java,Spring,Spring Boot,Spring Amqp,Spring Rabbit,我正在尝试为rabbitMq侦听器编写一个测试用例。我尝试使用spring rabbit测试,在运行测试时出现以下错误: 启动ApplicationContext时出错。显示条件报告的步骤 在启用“调试”的情况下重新运行应用程序。2018-03-06 17:10:50.113 错误14239---[main]o.s.boot.SpringApplication :应用程序运行失败 java.lang.IllegalStateException:另一个终结点已存在 已注册id为“响应队列” 在or
:应用程序运行失败 java.lang.IllegalStateException:另一个终结点已存在 已注册id为“响应队列” 在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) ~[spring-rabbit-2.0.2.RELEASE.jar:2.0.2.RELEASE] 我遵循的是[在他们共享的示例中,侦听器没有@组件,在理想情况下,它将是一个组件 现在,我的测试类还尝试在上面提到的错误中获取侦听器结果 有人能帮我吗 测试配置 测试 } 听众
是的…你需要和我们分享你的项目。这是一个简单的变体,让我们复制和播放来确定原因 现在我无法用非常简单的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;
}
}
似乎对我来说,我没有错过你的任何一点如何配置这一切在一起
@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);
}
}
@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;
}
}