【问题标题】:Is there an in memory messaging broker for functional testing of RabbitMq? [closed]是否有用于 RabbitMq 功能测试的内存消息传递代理? [关闭]
【发布时间】:2019-06-22 21:59:19
【问题描述】:
我需要编写涉及与 RabbitMq 交互的功能测试流程。但是一旦运行测试,我将不得不清除队列中的任何现有消息。由于 RabbitMq 是持久的,我需要一些内存替代 RabbitMq。就像我们为数据库使用 HSQL 的方式一样。
我曾尝试使用qpid 代理,但没有成功。
我正在使用 Spring Boot 框架。所以我只需要注入内存队列的bean而不是实际的rabbit mq。
【问题讨论】:
标签:
java
spring
testing
rabbitmq
functional-testing
【解决方案1】:
看看testcontainers。在这样的测试中运行RabbitMQ Docker image 非常容易。它将为每个测试类或方法重新启动,具体取决于您如何使用它。
这将为测试类启动一个运行rabbitmq:3.7 Docker 映像的容器。
public class AmqpReceiveServiceIntegrationTest {
@ClassRule
public static GenericContainer rabbitmqContainer =
new GenericContainer<>("rabbitmq:3.7").withExposedPorts(5672);
static ConnectionFactory factory;
static Connection connection;
static Channel sendChannel;
@BeforeClass
public static void beforeClass() throws IOException, TimeoutException {
factory = new ConnectionFactory();
factory.setHost(rabbitmqContainer.getContainerIpAddress());
factory.setPort(rabbitmqContainer.getFirstMappedPort());
connection = factory.newConnection();
sendChannel = connection.createChannel();
sendChannel.queueDeclare("hello", false, false, false, null);
}
@Test
public void sendIsOk() {
sendChannel.basicPublish("", "hello", null, "Hello World!.getBytes());
// assertions ...
}
}