5.15. Relative Execution Order of User Code and Extensions

When executing a test class that contains one or more test methods, a number of extension callbacks are called in addition to the user-supplied test and lifecycle methods.

See also: Test Execution Order

5.15.1. User and Extension Code

The following diagram illustrates the relative order of user-supplied code and extension code. User-supplied test and lifecycle methods are shown in orange, with callback code implemented by extensions shown in blue. The grey box denotes the execution of a single test method and will be repeated for every test method in the test class.

extensions lifecycle

User code and extension code

The following table further explains the sixteen steps in the User code and extension code diagram.

StepInterface/AnnotationDescription

1

interface org.junit.jupiter.api.extension.BeforeAllCallback

extension code executed before all tests of the container are executed

2

annotation org.junit.jupiter.api.BeforeAll

user code executed before all tests of the container are executed

3

interface org.junit.jupiter.api.extension.LifecycleMethodExecutionExceptionHandler #handleBeforeAllMethodExecutionException

extension code for handling exceptions thrown from @BeforeAll methods

4

interface org.junit.jupiter.api.extension.BeforeEachCallback

extension code executed before each test is executed

5

annotation org.junit.jupiter.api.BeforeEach

user code executed before each test is executed

6

interface org.junit.jupiter.api.extension.LifecycleMethodExecutionExceptionHandler #handleBeforeEachMethodExecutionException

extension code for handling exceptions thrown from @BeforeEach methods

7

interface org.junit.jupiter.api.extension.BeforeTestExecutionCallback

extension code executed immediately before a test is executed

8

annotation org.junit.jupiter.api.Test

user code of the actual test method

9

interface org.junit.jupiter.api.extension.TestExecutionExceptionHandler

extension code for handling exceptions thrown during a test

10

interface org.junit.jupiter.api.extension.AfterTestExecutionCallback

extension code executed immediately after test execution and its corresponding exception handlers

11

annotation org.junit.jupiter.api.AfterEach

user code executed after each test is executed

12

interface org.junit.jupiter.api.extension.LifecycleMethodExecutionExceptionHandler #handleAfterEachMethodExecutionException

extension code for handling exceptions thrown from @AfterEach methods

13

interface org.junit.jupiter.api.extension.AfterEachCallback

extension code executed after each test is executed

14

annotation org.junit.jupiter.api.AfterAll

user code executed after all tests of the container are executed

15

interface org.junit.jupiter.api.extension.LifecycleMethodExecutionExceptionHandler #handleAfterAllMethodExecutionException

extension code for handling exceptions thrown from @AfterAll methods

16

interface org.junit.jupiter.api.extension.AfterAllCallback

extension code executed after all tests of the container are executed

In the simplest case only the actual test method will be executed (step 8); all other steps are optional depending on the presence of user code or extension support for the corresponding lifecycle callback. For further details on the various lifecycle callbacks please consult the respective Javadoc for each annotation and extension.

All invocations of user code methods in the above table can additionally be intercepted by implementing InvocationInterceptor.

5.15.2. Wrapping Behavior of Callbacks

JUnit Jupiter always guarantees wrapping behavior for multiple registered extensions that implement lifecycle callbacks such as BeforeAllCallback, AfterAllCallback, BeforeEachCallback, AfterEachCallback, BeforeTestExecutionCallback, and AfterTestExecutionCallback.

That means that, given two extensions Extension1 and Extension2 with Extension1 registered before Extension2, any “before” callbacks implemented by Extension1 are guaranteed to execute before any “before” callbacks implemented by Extension2. Similarly, given the two same two extensions registered in the same order, any “after” callbacks implemented by Extension1 are guaranteed to execute after any “after” callbacks implemented by Extension2. Extension1 is therefore said to wrap Extension2.

JUnit Jupiter also guarantees wrapping behavior within class and interface hierarchies for user-supplied lifecycle methods (see Test Classes and Methods).

  • @BeforeAll methods are inherited from superclasses as long as they are not hidden or overridden. Furthermore, @BeforeAll methods from superclasses will be executed before @BeforeAll methods in subclasses.

    • Similarly, @BeforeAll methods declared in an interface are inherited as long as they are not hidden or overridden, and @BeforeAll methods from an interface will be executed before @BeforeAll methods in the class that implements the interface.
  • @AfterAll methods are inherited from superclasses as long as they are not hidden or overridden. Furthermore, @AfterAll methods from superclasses will be executed after @AfterAll methods in subclasses.

    • Similarly, @AfterAll methods declared in an interface are inherited as long as they are not hidden or overridden, and @AfterAll methods from an interface will be executed after @AfterAll methods in the class that implements the interface.
  • @BeforeEach methods are inherited from superclasses as long as they are not overridden. Furthermore, @BeforeEach methods from superclasses will be executed before @BeforeEach methods in subclasses.

    • Similarly, @BeforeEach methods declared as interface default methods are inherited as long as they are not overridden, and @BeforeEach default methods will be executed before @BeforeEach methods in the class that implements the interface.
  • @AfterEach methods are inherited from superclasses as long as they are not overridden. Furthermore, @AfterEach methods from superclasses will be executed after @AfterEach methods in subclasses.

    • Similarly, @AfterEach methods declared as interface default methods are inherited as long as they are not overridden, and @AfterEach default methods will be executed after @AfterEach methods in the class that implements the interface.

The following examples demonstrate this behavior. Please note that the examples do not actually do anything realistic. Instead, they mimic common scenarios for testing interactions with the database. All methods imported statically from the Logger class log contextual information in order to help us better understand the execution order of user-supplied callback methods and callback methods in extensions.

Extension1

  1. import static example.callbacks.Logger.afterEachCallback;
  2. import static example.callbacks.Logger.beforeEachCallback;
  3. import org.junit.jupiter.api.extension.AfterEachCallback;
  4. import org.junit.jupiter.api.extension.BeforeEachCallback;
  5. import org.junit.jupiter.api.extension.ExtensionContext;
  6. public class Extension1 implements BeforeEachCallback, AfterEachCallback {
  7. @Override
  8. public void beforeEach(ExtensionContext context) {
  9. beforeEachCallback(this);
  10. }
  11. @Override
  12. public void afterEach(ExtensionContext context) {
  13. afterEachCallback(this);
  14. }
  15. }

Extension2

  1. import static example.callbacks.Logger.afterEachCallback;
  2. import static example.callbacks.Logger.beforeEachCallback;
  3. import org.junit.jupiter.api.extension.AfterEachCallback;
  4. import org.junit.jupiter.api.extension.BeforeEachCallback;
  5. import org.junit.jupiter.api.extension.ExtensionContext;
  6. public class Extension2 implements BeforeEachCallback, AfterEachCallback {
  7. @Override
  8. public void beforeEach(ExtensionContext context) {
  9. beforeEachCallback(this);
  10. }
  11. @Override
  12. public void afterEach(ExtensionContext context) {
  13. afterEachCallback(this);
  14. }
  15. }

AbstractDatabaseTests

  1. import static example.callbacks.Logger.afterAllMethod;
  2. import static example.callbacks.Logger.afterEachMethod;
  3. import static example.callbacks.Logger.beforeAllMethod;
  4. import static example.callbacks.Logger.beforeEachMethod;
  5. import org.junit.jupiter.api.AfterAll;
  6. import org.junit.jupiter.api.AfterEach;
  7. import org.junit.jupiter.api.BeforeAll;
  8. import org.junit.jupiter.api.BeforeEach;
  9. /**
  10. * Abstract base class for tests that use the database.
  11. */
  12. abstract class AbstractDatabaseTests {
  13. @BeforeAll
  14. static void createDatabase() {
  15. beforeAllMethod(AbstractDatabaseTests.class.getSimpleName() + ".createDatabase()");
  16. }
  17. @BeforeEach
  18. void connectToDatabase() {
  19. beforeEachMethod(AbstractDatabaseTests.class.getSimpleName() + ".connectToDatabase()");
  20. }
  21. @AfterEach
  22. void disconnectFromDatabase() {
  23. afterEachMethod(AbstractDatabaseTests.class.getSimpleName() + ".disconnectFromDatabase()");
  24. }
  25. @AfterAll
  26. static void destroyDatabase() {
  27. afterAllMethod(AbstractDatabaseTests.class.getSimpleName() + ".destroyDatabase()");
  28. }
  29. }

DatabaseTestsDemo

  1. import static example.callbacks.Logger.afterEachMethod;
  2. import static example.callbacks.Logger.beforeAllMethod;
  3. import static example.callbacks.Logger.beforeEachMethod;
  4. import static example.callbacks.Logger.testMethod;
  5. import org.junit.jupiter.api.AfterAll;
  6. import org.junit.jupiter.api.AfterEach;
  7. import org.junit.jupiter.api.BeforeAll;
  8. import org.junit.jupiter.api.BeforeEach;
  9. import org.junit.jupiter.api.Test;
  10. import org.junit.jupiter.api.extension.ExtendWith;
  11. /**
  12. * Extension of {@link AbstractDatabaseTests} that inserts test data
  13. * into the database (after the database connection has been opened)
  14. * and deletes test data (before the database connection is closed).
  15. */
  16. @ExtendWith({ Extension1.class, Extension2.class })
  17. class DatabaseTestsDemo extends AbstractDatabaseTests {
  18. @BeforeAll
  19. static void beforeAll() {
  20. beforeAllMethod(DatabaseTestsDemo.class.getSimpleName() + ".beforeAll()");
  21. }
  22. @BeforeEach
  23. void insertTestDataIntoDatabase() {
  24. beforeEachMethod(getClass().getSimpleName() + ".insertTestDataIntoDatabase()");
  25. }
  26. @Test
  27. void testDatabaseFunctionality() {
  28. testMethod(getClass().getSimpleName() + ".testDatabaseFunctionality()");
  29. }
  30. @AfterEach
  31. void deleteTestDataFromDatabase() {
  32. afterEachMethod(getClass().getSimpleName() + ".deleteTestDataFromDatabase()");
  33. }
  34. @AfterAll
  35. static void afterAll() {
  36. beforeAllMethod(DatabaseTestsDemo.class.getSimpleName() + ".afterAll()");
  37. }
  38. }

When the DatabaseTestsDemo test class is executed, the following is logged.

  1. @BeforeAll AbstractDatabaseTests.createDatabase()
  2. @BeforeAll DatabaseTestsDemo.beforeAll()
  3. Extension1.beforeEach()
  4. Extension2.beforeEach()
  5. @BeforeEach AbstractDatabaseTests.connectToDatabase()
  6. @BeforeEach DatabaseTestsDemo.insertTestDataIntoDatabase()
  7. @Test DatabaseTestsDemo.testDatabaseFunctionality()
  8. @AfterEach DatabaseTestsDemo.deleteTestDataFromDatabase()
  9. @AfterEach AbstractDatabaseTests.disconnectFromDatabase()
  10. Extension2.afterEach()
  11. Extension1.afterEach()
  12. @BeforeAll DatabaseTestsDemo.afterAll()
  13. @AfterAll AbstractDatabaseTests.destroyDatabase()

The following sequence diagram helps to shed further light on what actually goes on within the JupiterTestEngine when the DatabaseTestsDemo test class is executed.

extensions DatabaseTestsDemo

DatabaseTestsDemo

JUnit Jupiter does not guarantee the execution order of multiple lifecycle methods that are declared within a single test class or test interface. It may at times appear that JUnit Jupiter invokes such methods in alphabetical order. However, that is not precisely true. The ordering is analogous to the ordering for @Test methods within a single test class.

Lifecycle methods that are declared within a single test class or test interface will be ordered using an algorithm that is deterministic but intentionally non-obvious. This ensures that subsequent runs of a test suite execute lifecycle methods in the same order, thereby allowing for repeatable builds.

In addition, JUnit Jupiter does not support wrapping behavior for multiple lifecycle methods declared within a single test class or test interface.

The following example demonstrates this behavior. Specifically, the lifecycle method configuration is broken due to the order in which the locally declared lifecycle methods are executed.

  • Test data is inserted before the database connection has been opened, which results in a failure to connect to the database.

  • The database connection is closed before deleting the test data, which results in a failure to connect to the database.

BrokenLifecycleMethodConfigDemo

  1. import static example.callbacks.Logger.afterEachMethod;
  2. import static example.callbacks.Logger.beforeEachMethod;
  3. import static example.callbacks.Logger.testMethod;
  4. import org.junit.jupiter.api.AfterEach;
  5. import org.junit.jupiter.api.BeforeEach;
  6. import org.junit.jupiter.api.Test;
  7. import org.junit.jupiter.api.extension.ExtendWith;
  8. /**
  9. * Example of "broken" lifecycle method configuration.
  10. *
  11. * <p>Test data is inserted before the database connection has been opened.
  12. *
  13. * <p>Database connection is closed before deleting test data.
  14. */
  15. @ExtendWith({ Extension1.class, Extension2.class })
  16. class BrokenLifecycleMethodConfigDemo {
  17. @BeforeEach
  18. void connectToDatabase() {
  19. beforeEachMethod(getClass().getSimpleName() + ".connectToDatabase()");
  20. }
  21. @BeforeEach
  22. void insertTestDataIntoDatabase() {
  23. beforeEachMethod(getClass().getSimpleName() + ".insertTestDataIntoDatabase()");
  24. }
  25. @Test
  26. void testDatabaseFunctionality() {
  27. testMethod(getClass().getSimpleName() + ".testDatabaseFunctionality()");
  28. }
  29. @AfterEach
  30. void deleteTestDataFromDatabase() {
  31. afterEachMethod(getClass().getSimpleName() + ".deleteTestDataFromDatabase()");
  32. }
  33. @AfterEach
  34. void disconnectFromDatabase() {
  35. afterEachMethod(getClass().getSimpleName() + ".disconnectFromDatabase()");
  36. }
  37. }

When the BrokenLifecycleMethodConfigDemo test class is executed, the following is logged.

  1. Extension1.beforeEach()
  2. Extension2.beforeEach()
  3. @BeforeEach BrokenLifecycleMethodConfigDemo.insertTestDataIntoDatabase()
  4. @BeforeEach BrokenLifecycleMethodConfigDemo.connectToDatabase()
  5. @Test BrokenLifecycleMethodConfigDemo.testDatabaseFunctionality()
  6. @AfterEach BrokenLifecycleMethodConfigDemo.disconnectFromDatabase()
  7. @AfterEach BrokenLifecycleMethodConfigDemo.deleteTestDataFromDatabase()
  8. Extension2.afterEach()
  9. Extension1.afterEach()

The following sequence diagram helps to shed further light on what actually goes on within the JupiterTestEngine when the BrokenLifecycleMethodConfigDemo test class is executed.

extensions BrokenLifecycleMethodConfigDemo

BrokenLifecycleMethodConfigDemo

Due to the aforementioned behavior, the JUnit Team recommends that developers declare at most one of each type of lifecycle method (see Test Classes and Methods) per test class or test interface unless there are no dependencies between such lifecycle methods.