I have this service class where I combine two Restcalls from Service A and B to one resonse 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));
}
}
My positive test works perfect. But in case I mock ServiceB to throw an exception, the recover is never called and the whole chain is terminated with the expected exception:
Mockito.when(serviceB.getSettings(id))
.thenThrow(new NotFoundException("Not found test"));
I assume I have a wrong understanding how onFailure works.... ?
CodePudding user response:
Your serviceB.getSettings()
call doesn't return a Uni
that fails -- it directly throws an exception. Look at the stack trace of that exception -- there's nothing on the stack that could possibly catch it and transform it to a "failed Uni
", it's just thrown too soon.
You need to arrange the mock so that it returns a Uni
that completes with an exception. Something like Uni.createFrom().failure(new NotFoundException("Not found test"));
.