Junit 5 功能测试 Micronaut 消息驱动应用程序

问题描述

我有一个 Rabbit MQ Micronaut 消息驱动应用程序。应用只包含消费者端,生产者端在另一个 REST API 应用上。

现在我只想在消费者端执行 JUnit 5 测试。尝试获得测试仅包含 Rabbit MQ 侦听器的消息驱动应用程序的最佳想法

@RabbitListener
public record CategoryListener(IRepository repository) {

@Queue(ConstantValues.ADD_CATEGORY)
    public Categoryviewmodel Create(Categoryviewmodel model) {
        LOG.info(String.format("Listener --> Adding the product to the product collection"));
        Category category = new Category(model.name(),model.description());
        return Single.frompublisher(this.repository.getCollection(ConstantValues.PRODUCT_CATEGORY_COLLECTION_NAME,Category.class)
                .insertOne(category)).map(success->{
            return new Categoryviewmodel(
                    success.getInsertedId().asObjectId().getValue().toString(),category.getName(),category.getDescription());
        }).blockingGet();
    }
}

经过一番研究,我发现我们可以使用 Testcontainers 进行集成测试,就我而言,生产者和接收者在不同的服务器上。那么我是否需要在测试环境中为每个 RabbitClient 创建 RabbitListener 或者有什么方法可以模拟 RabbitClient

@MicronautTest
@Testcontainers
public class CategoryListenerTest {

    @Container
    private static final RabbitMQContainer RABBIT_MQ_CONTAINER = new RabbitMQContainer("rabbitmq")
            .withExposedPorts(5672,15672);

    @Test
    @displayName("Rabbit MQ container should be running")
    void rabbitMqContainerShouldBeRunning() {
        Assertions.assertTrue(RABBIT_MQ_CONTAINER.isRunning());
    }
}

执行 Micronaut 消息驱动应用程序功能测试的最佳方法是什么?在这个问题中,我在另一个应用程序上有一个生产者。所以我不能注入 PRODUCER 客户端。如何在 LISTENER 端测试此功能

解决方法

使用@RabbitClient创建生产者或直接使用java api