I am converting some code to asynchronous. The initial unit test used the @Test(expected = MyExcpetion.class) annotation @Test(expected = MyExcpetion.class) , but I don't think this will work because the exception I want to state is wrapped in java.util.concurrent.ExcutionException . I really tried to call my future like this, but my statement still fails, and I don't like what I had to add to return null
myApiCall.get(123).exceptionally((ex) -> { assertEquals(ex.getCause(),MyCustomException.class) return null }
I also tried this fragrance but still didn't work
myApiCall.get(123).exceptionally((ex) -> { assertThat(ex.getCause()) .isInstanceOF(MyException.class) .hasMessage("expected message etc") return null; }
My API just throws an exception if it cannot find the identifier. How should I test this correctly? Can I use this original annotation?
my api call reaches db value on startup. In this test, I set my future to return an error, so in fact it does not try to communicate with anything. the checked code is as follows
public class myApiCall { public completableFuture get(final String id){ return myService.getFromDB(id) .thenApply(
in unit test, I force myService.getFromDB(id) to return bad data so that I can check the exception, and also save this unit test, not reaching db, etc.
Barry source share