0

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

nano_nano
  • 12,351
  • 8
  • 55
  • 83

1 Answers1

1

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"));.

Ladicek
  • 5,970
  • 17
  • 20
  • ya that is the solution. returning exactly that solved my problem. thank you very much. – nano_nano Sep 12 '22 at 10:18
  • now my integration test is failing. do I need to provide a exceptionmapper to convert all kinds of exceptions to Uni Exception ??? – nano_nano Sep 13 '22 at 07:07
  • You don't provide any information for anyone to be able to help. Maybe ask a new question? – Ladicek Sep 13 '22 at 08:17