У меня есть следующий метод.Он отлично работает и может протестировать остальные части кода.Но как я могу проверить / смоделировать тот блок catch, который перехватывает ExecutionException?Я хочу приземлиться в этом блоке ловли.Я использую Mockito, чтобы издеваться над другими частями, но это не так, как если бы я мог высмеивать этот блок try в этом случае, или я могу?
private static ExecutorService executor = Executors.newFixedThreadPool(1);
public Detail create(String num){
return new Detail(){
private final Future<String> number = executor.submit(() -> someAutowiredClass.method(num));
@Override
public String getNumber() {
try {
return this.number.get();
} catch (InterruptedException e) {
throw new CustomException();
} catch (ExecutionException e) {
throw new CustomException2();
// I want to land inside here for my test. How do I do that?
}
}
};
}
Вот как я сейчас написал тест для прерванной части.
@Test
public void myTest() {
String num = "123";
String number = "456";
when(someAutowiredClass.method(num)).thenReturn(456);
Detail detail = myClass.create(num);
Thread.currentThread().interrupt();
try {
assertEquals(number, detail.getNumber());
}catch (CustomException e){
// some assertions.
}
}