I have a very strange corner case where a future doesn't complete as expected once every couple of thousands of runs. Only way I'm able to consistently trigger the fault is to have the test run on repeat until it fails.
Said test:
public class TestFunctionality(){
@Mock private MockIntegrationA mockIntegrationA;
@InjectMocks private MyHandler unitUnderTest;
@BeforeEach
public void init(){
MockitoAnnotations.openMocks(this);
}
@Test
public void testManagerFailThenSucceed(){
when(mockIntegrationA.callRestMethod(any())
.thenThrow(new RuntimeException(""))
.thenReturn(CompletableFuture.completedFuture(null));
CompletableFuture<Void> result = unitUnderTest.invokeMethod();
assertTrue(result.isCompletedExceptionally());
result = unitUnderTest.callFunction();
assertFalse(result.isCompletedExceptionally());
}
}
The first assert in above test is what trips up when the test does go bad. Is there something that isn't cleaned properly or what is causing this edge case where first result isn't completed exceptionally?
I can't ack comment as answer but @tgdavies comment about runAsync was correct. I changed it to a completedFuture and now I'm not able to trigger this behavior. Thanks a lot for the help! Spent hours on trying to solve it