I have made my function async with executor service but while executing test cases are not executing the executor service method.
The update code similar to:
void method1(Status status) {
executorService.submit(() -> {
try () {
method2();
} catch (Exception exception) {
throw new CustomException(exception.getMessage(), exception);
}
});
}
while debuging the exixting testcase i found that the executorService.submit was skipped. I tried to mock the submit method but not working.
@Test(expected = CustomException.class)
public void testSatuse() throws Exception {
Status mocktatus = getStatusMock("XXX", "XXXX");
PowerMockito.doNothing().when(httpResponse).close();
CustomException exception = PowerMockito.mock(CustomException.class);
PowerMockito.when(httpClient.execute(Mockito.any())).thenReturn(httpResponse);
Future<String> futureMock = Mockito.mock(Future.class);
PowerMockito.when(executorServiceMock.submit(Mockito.any(Runnable.class)))
.thenAnswer(invocation -> {
Callable<String> task = invocation.getArgument(0);
return futureMock;
});
PowerMockito.when(httpResponse.getStatusLine()).thenThrow(exception);
myClassName.method1(mockStatus);
}
If i change the @Test(expected = CustomException.class) to @Test(expected = Exception.class) it will work but I can not do that. What should be the best practice to solve this so that the method2() is invoked.