Skip to content

Allow @BeforeAll and @AfterAll methods to be non-static #419

Closed
@mfulton26

Description

@mfulton26

Overview

#88 explains why @BeforeAll and @AfterAll methods currently must be static; however, the question arises again and again.

Related Issues

Proposal

Reintroduce support for @TestInstance as was present in the JUnit 5 Prototype, thereby allowing developers and third party extension authors to make use of the feature as well. In other words, we do not want to limit use of this feature to JUnit Jupiter itself.

Open Issues

Special caution must be taken with regard to @Nested test classes in terms of the lifecycle of the test instance against which @BeforeAll and @AfterAll methods are executed.

Original Issue Description

Can support be added for @BeforeAll and @AfterAll methods in @Nested test classes?

For example:

class Tests {
    @Test
    void something() {
    }

    @BeforeAll(nested = SubTests.class)
    static void setUp() {
    }

    @Nested
    class SubTests {

        // ...

Deliverables

  • Reintroduce support for @TestInstance as was present in the JUnit 5 prototype.
    • Make @TestInstance an @Inherited annotation to support lifecycle inheritance semantics within a test class hierarchy.
  • Add support for @TestInstance for @Nested test classes.
    • Ensure proper behavior for @Nested test classes in terms of the lifecycle of the test instance against which @BeforeAll and @AfterAll methods are executed.
  • Allow @BeforeAll and @AfterAll methods to be non-static.
  • Move the getTestInstance() method from TestExtensionContext to ExtensionContext.
    • Ensure that the proper test instance is supplied to container-level extensions via the ExtensionContext.
    • Ensure that the proper test instance is supplied to TestInstancePostProcessor extensions via the ExtensionContext as well.
    • Ensure that the proper test instance is supplied to TestTemplateContainerExtensionContext extensions via the ExtensionContext as well.
  • Add documentation to the User Guide for the new features.
  • Update existing documentation in the User Guide which states that @BeforeAll and @AfterAll methods must be static.
  • Update existing JavaDoc which states that @BeforeAll and @AfterAll methods must be static.
  • Update the release notes in the User Guide describing the new features.
  • Update the release notes in the User Guide describing breaking changes.

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions