4

How will I make sure that my @After method runs even if @Test method throws an exception which is unhandled or this is actually internally done by JUnit?

AlwaysNoob
  • 75
  • 2
  • 9

2 Answers2

10

JUnit runs methods annotated with @After after each test case regardless of thrown exceptions.

To quote the JUnit documentation:

All @After methods are guaranteed to run even if a Before or Test method throws an exception.

migu
  • 1,371
  • 1
  • 14
  • 23
2

Yes, an @After method is always run, even if an exception is thrown in a @Test method.

The test will fail if it isn't configured with @Test(expected=ExceptionClass.class).

daniu
  • 14,137
  • 4
  • 32
  • 53