I am running a JUnit 4 test using @RunWith(value = Parameterized.class)
. This works fine, no problems there. However, when any of my 34 tests timeout, I only get the message java.lang.Exception: test timed out after 15000 milliseconds
. I want it to also show the parameter of the test.
I have even tried to do it like the code below (which I know is a horrible solution for most cases, I just wanted to see if I could get the message to show any time at all), but that did not work, it still resulted in the message above.
private String parameter;
@Test(timeout = 15000)
public void solveAll() {
try {
// ... do something that might take a long time
}
catch (Throwable e) {
Assert.fail(this.parameter + " failed! Because of " + e.getMessage());
}
}
How can I make JUnit also show this.parameter
when the test results in a timeout ?
Here is a very simple example test class that shows this problem:
public class ShowMyMessageTest {
@Test(timeout=1000)
public void test() {
try {
Thread.sleep(3000);
}
catch (Throwable e) {
Assert.fail("Timeout reached with value 42");
}
}
}
With this ShowMyMessageTest
I sometimes get the expected "Timeout reached with value 42", and sometimes I get only "java.lang.Exception: test timed out after 1000 milliseconds". I want to always get "Timeout reached with value 42" in this case.