Can I control JUnit timeout?

Suppose I have a JUnit unit test, which has two parts, and I don’t want to separate them into separate @Test methods. Suppose also that I want a timeout parameter for a test.

How can I change / intercept / control a timeout approval error message to indicate which part of the test has been crossed out?

Here's an attempt that doesn't work:

 @Test(timeout = 1000) public void test() { try { // part one of the test } catch (Throwable e) { Assert.fail("Part one failed"); } try { // part two of the test } catch (Throwable e) { Assert.fail("Part two failed"); } } 
+4
source share
2 answers

Just by reading the documentation, no. A timeout automatically fails the test. This does not throw an exception - it makes a test runner. You might be able to write your own test runner, but you will still have trouble figuring out where he failed. I would suggest structuring your test in different ways. Perhaps you have your own timer that just failed the test.

+2
source

The JUnit timeout function is pretty limited, as you know. For synchronization tests that require a bit more features, I used awaitility . You can use "named wait" to configure the message:

 with().pollInterval(ONE_HUNDERED_MILLISECONDS) .and().with().pollDelay(20, MILLISECONDS) .await("customer registration").until( costumerStatus(), equalTo(REGISTERED) ); 
+1
source

All Articles