How to define JUnit test time-out at runtime (i.e.

2019-07-03 23:32发布

I want to run a unit test with a time-out that is defined at runtime. I want to define a time-out for a specific test only, not the whole class.

I saw these are the ways to set the time out:

@Rule
public Timeout globalTimeout = new Timeout(10000); // 10 seconds max per method tested

or

@Test(timeout=100) public void infinity() {
   while(true);
}

but when I run this code, no exception is thrown. I want to identify when the test failed due to timeout.

public Timeout testTimeout;

private void setTestTimeOut() {
    if (!Strings.isNullOrEmpty(testTimeOut)) {
        testTimeout = new Timeout(Integer.parseInt(testTimeOut));
    }
}

How can I catch the exception? Wrap the main method with try-catch(InterruptException)?

2条回答
放荡不羁爱自由
2楼-- · 2019-07-03 23:55

Add a TestWatcher rule that decides, at run time, whether to apply a timeout or not:

@Rule
public TestWatcher watcher = new TestWatcher() {
  @Override
  public Statement apply(Statement base, Description description) {
    // You can replace this hard-coded test name and delay with something
    // more dynamic
    if (description.getMethodName().equals("infinity")) {
      return new FailOnTimeout(base, 200);
    }

    return base;
  }
};

Your original approach didn't work because JUnit rules take effect before the test code begins to run, so any attempts to adjust your Timeout object within your test are too late.

查看更多
淡お忘
3楼-- · 2019-07-03 23:58

The @Test(timeout=xxx) will not throw a TimeoutException, it will fail the test.

Could you specify in more detail what you would like to test?

查看更多
登录 后发表回答