Reputation: 424993
Suppose I have a JUnit unit test that has two parts to it and I don't want to separate the parts into separate @Test
methods. Suppose also that I want a timeout
parameter for the test.
How can I change/intercept/control the timeout assertion failure message to indicate which part of the test timed out?
Here's an attempt that does not 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");
}
}
Upvotes: 4
Views: 847
Reputation: 516
Just reading up on the documentation, no. The timeout automatically fails the test. This isn't throwing an exception -- it's done by the test runner. It's possible that you could write your own test runner, but you're still going to have troubles finding out where it failed. I would suggest structuring your test differently. Possibly have your own timer that just fails the test.
Upvotes: 2
Reputation: 26161
JUnit's timeout feature is rather limited as you've found out. For timing related tests that require a little more features I've used awaitility. You can use "named await" to customize the message:
with().pollInterval(ONE_HUNDERED_MILLISECONDS)
.and().with().pollDelay(20, MILLISECONDS)
.await("customer registration").until(
costumerStatus(), equalTo(REGISTERED)
);
Upvotes: 1