How to test that a method should take more than X seconds to finish(with JUnit)?

点点圈 提交于 2020-01-15 08:11:13

问题


Basically I need the opposite behaviour of the @Test(timeout=X) annotation.

The problem I want to solve is to detect in some way that the method never ends (as a right behaviour). I am assuming that if the method didn't stop after X seconds, I am sure "it will never end".

Thanks!


回答1:


You could try this:

@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
}


来源:https://stackoverflow.com/questions/1393080/how-to-test-that-a-method-should-take-more-than-x-seconds-to-finishwith-junit

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!