title: How can I test exception in completable future?
body: <p>I have been converting some code to be asynchronous. The original unit test used the annotation <code>@Test(expected = MyExcpetion.class)</code> but I don't think this will work because the exception I want to assert on is wrapped in <code>java.util.concurrent.ExcutionException</code> . I did try calling my future like this but my assertion is still failing and I don't love that I had to add in <code>return null</code></p>
  
  <pre><code>myApiCall.get(123).exceptionally((ex) -&gt; {
   assertEquals(ex.getCause(),MyCustomException.class)
   return null
  }
  </code></pre>
  
  <p>I also tried this flavor but still not working </p>
  
  <pre><code>myApiCall.get(123).exceptionally((ex) -&gt; {
   assertThat(ex.getCause())
    .isInstanceOF(MyException.class)
    .hasMessage("expected message etc")
   return null;
  }
  </code></pre>
  
  <p>My API just throws exception if it can't find id.  How should I be properly testing this? Can I use that original annotation in anyway? </p>
  
  <p>my api call reaches out to db when run. In this test I am setting up my future to return an error so it doesn't actually try to communicate with anything. the code under test looks like this </p>
  
  <pre><code> public class myApiCall  { 
     public completableFuture get(final String id){
     return myService.getFromDB(id)
      .thenApply( 
         //code here looks at result and if happy path then returns it after 
         //doing some transformation 
        //otherwise it throws exception 
     )
    }
   }
  </code></pre>
  
  <p>in the unit test I force <code>myService.getFromDB(id)</code> to return bad data so I can test exception and also keep this a unit test don't reach out to db etc.</p>