[英]Junit 5 functional testing the Micronaut Messaging-Driven Application
我有一個 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
進行集成測試,在我的例子中,生產者和接收者在不同的服務器上。 所以我需要在測試環境中為每個RabbitListener
創建RabbitClient
還是有什么方法可以模擬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 消息驅動應用程序功能測試的最佳方法是什么? 在這個問題中,我在另一個應用程序上有一個生產者。 所以我不能注入生產者客戶端。 如何在 LISTENER 端測試這個 function?
使用@RabbitClient
創建生產者或直接使用 java api
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.