0

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?

San Jaisy
  • 15,327
  • 34
  • 171
  • 290

1 Answers1

1

Create producers with @RabbitClient or use the java api directly

James Kleeh
  • 12,094
  • 5
  • 34
  • 61
  • Well I did create a producer but while injecting the producer to the test I am facing an issue described in this question https://stackoverflow.com/questions/68697474/unexpected-error-loading-bean-definition-with-micronaut-test-for-rabbitmq-listen – San Jaisy Aug 08 '21 at 08:02
  • Then why did you delete it? Would need a sample app to debug that issue – James Kleeh Aug 16 '21 at 17:24