У меня есть следующий метод, который отлично работает.Я пытаюсь проверить сценарий, где выбрасывается InterruptedException.Вот как я сейчас тестирую, и это работает, если я только запускаю этот единственный тест.Но если бы я должен был выполнить все оставшиеся 5 тестов в своем тестовом классе, некоторые из них начинаются с ошибками.Все тесты проходят, когда я запускаю их по отдельности, поэтому, очевидно, мое прерывание Thread в тесте влияет на другие тесты.Как я могу написать свой тест так, чтобы он не влиял на другие тесты?
@Component
class A{
@Autowired
private Helper helper;
private static ExecutorService executor = Executors.newFixedThreadPool(10);
// static variable in class
private final Future<String> number = executor.submit(() -> helper.method());
//method to be tested
public String getNumber() {
try {
return this.number.get();
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
throw new CustomException1();
} catch (ExecutionException e) {
throw new CustomException2();
}
}
}
@RunWith(MockitoJUnitRunner.class)
clas ATest{
@InjectMocks
private A a;
@Mock
private Helper helper;
// this is my test method which passes when ran individually. But will affect other tests if everything is ran same time.
@Test
public void testMethod() {
when(helper.method()).thenReturn("222");
String num = a.getNumber();
// doing this on purpose so I would land inside the catch. This line is causing issues.
Thread.currentThread().interrupt();
try {
assertEquals("222", num);
}catch (CustomException1 e){
// I will land here for this test as expected
// do some assertions
}
// Thread.currentThread().interrupt(); // Tried calling it again here to clear the flag but doesn't work.
}
// another few tests .....
}