2

@Test(timeout=X)基本上我需要注释的相反行为。

我要解决的问题是以某种方式检测该方法永远不会结束(作为正确的行为)。我假设如果该方法在 X 秒后没有停止,我确信“它永远不会结束”。

谢谢!

4

1 回答 1

4

你可以试试这个:

@Test
public void methodDoesNotReturnFor5Seconds() throws Exception {
  Thread t = new Thread(new Runnable() {
    public void run() {
      methodUnderTest();
    }
  });
  t.start();
  t.join(5000);
  assertTrue(t.isAlive());
  // possibly do something to shut down methodUnderTest
}
于 2009-09-08T10:03:29.083 回答