[英]onFailure never called in quarkus reactive chain
我有這項服務 class,我將來自服務 A 和 B 的兩個 Restcall 組合到一個響應 object。
@ApplicationScoped
public class ResponseHandler {
@Inject
Mapper mapper;
@Inject
ServiceA serviceA;
@Inject
ServiceB serviceB;
public Uni<Dto> handle(String id, String language) {
return Uni.combine().all()
.unis(serviceA.getProducts(id),
serviceB.getSettings(id).onFailure().recoverWithItem(SettingsResponse.builder().build()))
.combinedWith((productsResponse, settingsResponse) -> mapper.map(productsResponse, settingsResponse, language));
}
}
我的陽性測試效果很好。 但是,如果我模擬 ServiceB 以引發異常,則永遠不會調用恢復,並且整個鏈因預期的異常而終止:
Mockito.when(serviceB.getSettings(id))
.thenThrow(new NotFoundException("Not found test"));
我假設我對 onFailure 的工作原理有錯誤的理解......?
您的serviceB.getSettings()
調用不會返回失敗的Uni
- 它直接引發異常。 查看該異常的堆棧跟蹤——堆棧上沒有任何東西可以捕獲它並將其轉換為“失敗的Uni
”,它只是被拋出得太早了。
您需要安排模擬,以便它返回一個以異常完成的Uni
。 像Uni.createFrom().failure(new NotFoundException("Not found test"));
.
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.