[英]How to assert that an Uni throws a given error?
我有一個反應性 Quarkus 項目,我正在嘗試測試返回Uni<User>
的方法。 該方法如下所示:
public Uni<User> searchUser(ObjectId userId) {
return userRepository
.findById(userId)
.map(user -> {
if (user != null) {
return user;
} else {
throw new BusinessException("Could not find an user with the given id.");
}
});
到目前為止,一切都很好。 現在我正在嘗試測試一種方法,該方法斷言當存儲庫在findById
上返回null
時會引發錯誤。 根據mutiny 的文檔,這是斷言 Uni 拋出錯誤的方式:
Multi<Object> multi = Multi.createFrom().failure(() -> new IOException("Boom"));
AssertSubscriber<Object> subscriber = multi
.subscribe().withSubscriber(AssertSubscriber.create(10));
subscriber.assertFailedWith(IOException.class, "Boom");
這是我改編的版本:
@Test
public void shouldThrowAnErrorWhenUserIsNotFound() {
Mockito.when(userRepository.findById(Mockito.any())).thenReturn(Uni.createFrom().nullItem());
AssertSubscriber subscriber = service.searchUser(new ObjectId()).subscribe().withSubscriber(AssertSubscriber.create());
subscriber.assertFailedWith(BussinessException.class, "Could not find an user with the given id.");
}
但它沒有建立。 我在.subscribe().withSubscriber()
調用中收到此錯誤: Inferred type 'S' for type parameter 'S' is not within its bound; should implement 'io.smallrye.mutiny.subscription.UniSubscriber<? super br.com
Inferred type 'S' for type parameter 'S' is not within its bound; should implement 'io.smallrye.mutiny.subscription.UniSubscriber<? super br.com
Inferred type 'S' for type parameter 'S' is not within its bound; should implement 'io.smallrye.mutiny.subscription.UniSubscriber<? super br.com
。 我怎樣才能解決這個問題? 還是有另一種方法來編寫這樣的測試?
刪除AssertSubscriber
分配修復了類型錯誤,現在測試按預期工作。
@Test
public void shouldThrowAnErrorWhenUserIsNotFound() {
Mockito.when(userRepository.findById(Mockito.any())).thenReturn(Uni.createFrom().nullItem());
service.searchUser(new ObjectId())
.subscribe()
.withSubscriber(UniAssertSubscriber.create())
.assertFailedWith(BusinessException.class, "Could not find an user with the given id.");
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.