我发现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