Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
248 views
in Technique[技术] by (71.8m points)

java - 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?

question from:https://stackoverflow.com/questions/65557767/junit-5-functional-testing-the-micronaut-messaging-driven-application

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Create producers with @RabbitClient or use the java api directly


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...