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?
Asked
Active
Viewed 3,025 times
4
-
1*... or this is actually internally done by JUnit?* Try it out and you will know. – Thomas Fritsch Feb 12 '18 at 10:02
2 Answers
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