Search code examples
junitexpected-exception

How to indicate that I don't expect an exception in JUnit?


In google test, we have an

EXPECT_NO_THROW(x.foo());

How can I do this in JUnit? The thing I want to avoid is having to write a try/catch block, and to specify that my test function would throw anything. Java forces me to declare a throws clause with the test function...

Is it possible?


Solution

  • You don't need to. If your method throws an exception, the test will fail, which is what you want.

    In this case, it's acceptable to have your test declaring throws Exception:

    @Test public void testMe() throws Exception {
      x.foo();
    }
    

    This means (to me) that you're not expecting any exceptions.