Search code examples
javaunit-testingjunitjunit4parameterized-unit-test

Adding more information when JUnit 4 timeouts with parameterized runner


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.


Solution

  • This is a bit of a hack, but you could use @After to check the state of the parameter:

    @RunWith(Parameterized.class) public class FooTest {
      private boolean flag = true;
      private String param;
    
      public FooTest(String param) {
        this.param = param;
      }
    
      @Test(timeout = 1000) public void test() {
        while(true == flag);
        param = null;
      }
    
      @After public void after() {
        Assert.assertNull("Problem:" + param, param);
      }
    
      @Parameters public static Collection<Object[]> params() {
        Object[][] params = { { "foo" } };
        return Arrays.asList(params);
      }
    }
    

    An alternative is to write your own runner.