Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In JUnit, will @After method run, when @Test method throws an exception which is unhandled?

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?

like image 781
AlwaysNoob Avatar asked Jan 03 '23 20:01

AlwaysNoob


2 Answers

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.

like image 185
migu Avatar answered Jan 05 '23 16:01

migu


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).

like image 26
daniu Avatar answered Jan 05 '23 15:01

daniu