Search code examples
javajunitjunit5micronautmicronaut-test

Junit 5 functional testing the Micronaut Messaging-Driven Application


I have a Rabbit MQ Micronaut Messaging-Driven application. The application only contains the Consumer side, Producer side is on another REST API application.

Now I want to perform JUnit 5 testing with the consumer side only. Trying to get the best idea to test the Messaging-Driven application that contains only the Rabbit MQ Listener

@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();
    }
}

After some research, I found that we can use Testcontainers for integration testing, In my case, the Producer and receiver are on a different server. So do I need to create RabbitClient for each RabbitListener in the test environment or is there any way to mock 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());
    }
}

What is the best way to perform functional tests of Micronaut Messaging-Driven Application? In this question, I have a PRODUCER on another application. So I can't inject a PRODUCER client. How can I test this function on the LISTENER side?


Solution

  • Create producers with @RabbitClient or use the java api directly