是否有用于 RabbitMq 功能测试的内存消息传递代理?

Is there an in memory messaging broker for functional testing of RabbitMq?

我需要编写涉及与 RabbitMq 交互的功能测试流程。但是一旦测试 运行 我将不得不清除队列中的任何现有消息。由于 RabbitMq 是持久的,我需要一些内存替代品来替代 RabbitMq。就像我们为数据库使用 HSQL 的方式一样。

我曾尝试使用 qpid 经纪人,但没有成功。

我正在使用 spring 引导框架。所以我只需要注入内存队列的 bean 而不是实际的 rabbit mq。

看看testcontainers. Running a RabbitMQ Docker image这样的考试很容易。它将针对每个测试 class 或方法重新启动,具体取决于您如何使用它。

这将启动一个容器 运行 rabbitmq:3.7 Docker 图像用于测试 class。

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 ...
  }
}