Quick revision
The short answer
For a class with two tests, JUnit runs @BeforeAll once, then for each test @BeforeEach, the test and @AfterEach, and finally @AfterAll once. @AfterEach still runs when the test fails or throws, and even when @BeforeEach itself throws, in which case the test body is skipped. With inheritance, superclass @BeforeEach methods run before subclass ones, and @AfterEach methods run in the reverse order.
What to remember
In this answer, we printed the lifecycle of a class with two tests: @BeforeAll once, @BeforeEach, test and @AfterEach for each test, and @AfterAll once. We saw that @AfterEach runs even when the test throws and even when @BeforeEach throws, in which case the test body is skipped, so cleanup code must handle half-initialized state. With inheritance, superclass setup runs first and subclass teardown runs first.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
For a class with two tests, JUnit runs @BeforeAll once, then for each test @BeforeEach, the test and @AfterEach, and finally @AfterAll once. @AfterEach still runs when the test fails or throws, and even when @BeforeEach itself throws, in which case the test body is skipped. With inheritance, superclass @BeforeEach methods run before subclass ones, and @AfterEach methods run in the reverse order.
Why this question is asked
Lifecycle order sounds like trivia, but it decides whether resources are cleaned up, whether a failing setup leaves a database dirty, and whether a base test class does its work at the right moment. Interviewers ask it as a small coding question because it is easy to check by printing and the follow-up about exceptions shows whether we understand cleanup guarantees. In this answer, we will print the order for a class with two tests, see what happens when a test throws and when setup throws, and look at how inheritance changes the order.
1A Quick Recap of the Lifecycle Annotations
JUnit Jupiter has four lifecycle annotations around @Test. @BeforeAll runs once before all tests of the class, for expensive shared setup, and @AfterAll runs once after all of them. @BeforeEach runs before every single test, for fresh setup each time, and @AfterEach runs after every test, for cleanup. With the default lifecycle, the All methods are static and the Each methods are instance methods.
2Printing the Order for Two Tests
The quickest way to learn the order is to make every method print its name. Here is a class with two tests.
- @BeforeAllonce
- @BeforeEachfor test 1
- Test 1
- @AfterEachfor test 1, even if it failed
- @BeforeEachfor test 2
- Test 2
- @AfterEachfor test 2
- @AfterAllonce
8 steps in order. Play them, or step through them one at a time.
So @BeforeAll and @AfterAll appear once, and the Each methods wrap each test. Notice that the order of first and second is not something we should rely on: JUnit uses a deterministic but deliberately non-obvious order unless we set @TestMethodOrder. The wrapping, however, is guaranteed.
3What Happens When a Test Throws
Now let us make the first test fail. An assertion failure is just an exception, an AssertionFailedError, so this is the same as any exception thrown from the test body.
@AfterEach still runs, and the second test still runs normally. The reason is that JUnit treats @AfterEach as cleanup, like a finally block: it records the test's exception, runs the after methods, and then reports the failure. This is what lets us close files, delete temporary rows or reset a clock in @AfterEach and trust that it happens.
4What Happens When @BeforeEach Throws
A failing setup is the case people get wrong most often. If @BeforeEach throws, JUnit does not run the test body, because the test would run on a half-prepared state. But it still runs the @AfterEach methods, and it reports the test as failed with the setup exception.
So our @AfterEach must be written defensively. If setup creates a temporary directory and then fails while opening a connection, @AfterEach may find the directory but no connection, so it should check for null before closing things. Similarly, if @BeforeAll fails, none of the tests in the class run, but @AfterAll still runs.
5Order With a Base Test Class
Many projects put common setup in an abstract base class. JUnit runs the superclass @BeforeEach methods before the subclass ones, so the base can prepare things the subclass relies on, and it runs @AfterEach methods in the reverse order, subclass first, so the subclass cleans up its own parts before the base tears down the shared ones. The same holds for @BeforeAll and @AfterAll.
This mirrors constructors and destructors: build from the outside in and tear down from the inside out. One detail to remember is that if the subclass overrides a base lifecycle method with the same signature, only the override runs. Within one class, the order of several @BeforeEach methods is deterministic but not guaranteed to follow source order, so we should not split dependent setup across two methods in the same class.
6Follow-up questions
Does @AfterAll run if a test fails?
Yes. Test failures do not stop the class lifecycle. @AfterAll runs once after all tests have finished, whatever their results, and it also runs when @BeforeAll failed, so cleanup there should also handle partly initialized state.
How do these annotations map to JUnit 4?
@BeforeAll and @AfterAll replace @BeforeClass and @AfterClass, and @BeforeEach and @AfterEach replace @Before and @After. The behaviour on failures is the same idea, cleanup runs even when the test fails.
What if @AfterEach itself throws?
The test is reported as failed with that exception. If the test had already failed, the cleanup exception is added as a suppressed exception to the original failure, so both are visible in the report.
7Conclusion
In this answer, we printed the lifecycle of a class with two tests: @BeforeAll once, @BeforeEach, test and @AfterEach for each test, and @AfterAll once. We saw that @AfterEach runs even when the test throws and even when @BeforeEach throws, in which case the test body is skipped, so cleanup code must handle half-initialized state. With inheritance, superclass setup runs first and subclass teardown runs first.
Open with
The order is @BeforeAll once, then for the first test @BeforeEach, the test and @AfterEach, then the same three for the second test, and finally @AfterAll once. So for two tests I get one before-all, two before-each, two tests, two after-each and one after-all.
Then, if they want more
I would not rely on which test runs first unless I set @TestMethodOrder, but the wrapping is guaranteed. If a test throws, whether it is an assertion failure or any other exception, JUnit records the failure and still runs @AfterEach, like a finally block, and then carries on with the next test.
If @BeforeEach throws, the test body is skipped, the test is reported as failed, and @AfterEach still runs. That is why I write cleanup defensively and check for null before closing things.
With a base test class, superclass @BeforeEach methods run before the subclass ones, and @AfterEach runs in reverse, subclass first then superclass, so things are built from the outside in and torn down from the inside out. @AfterAll also runs regardless of test results.
About this answer
- Last updated
- 12 November 2026
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- JUnit user guide
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "In what order do these run for a class with two test methods: @BeforeAll, @BeforeEach...": https://scrivio-interview-library.netlify.app/java/junit/a2eb8330b3/