我發現JUnit的文檔在下面的例子:JUnit和InterruptedException的
public static class HasGlobalLongTimeout {
@Rule
public Timeout globalTimeout= new Timeout(20);
@Test
public void run1() throws InterruptedException {
Thread.sleep(100);
}
@Test
public void infiniteLoop() {
while (true) {}
}
}
我明白,只要JUnit的嘗試中斷第一次測試,它會中斷線程在那裏上運行,它會拋出一個InterruptedException,導致測試結束。
但是第二個測試(infiniteLoop)呢?它不會拋出任何東西。超時後如何停止?
我從你的評論中瞭解到(在查看源代碼之後),junit會嘗試以超時方式加入該線程,並檢查它是否在超時後仍在運行。如果是,測試將失敗。即,即使測試在後臺運行,它在失敗的加入呼叫之後也會失敗。 – sapito