Closed
Description
Overview
#88 explains why @BeforeAll
and @AfterAll
methods currently must be static
; however, the question arises again and again.
Related Issues
- Introduce first-class support for scenario tests #48
- Document that @BeforeAll & @AfterAll are unsupported in @Nested test classes #88
- Introduce configuration parameter for setting default test instance lifecycle semantics #905
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.
- Make
- 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.
- Ensure proper behavior for
- Allow
@BeforeAll
and@AfterAll
methods to be non-static. - Move the
getTestInstance()
method fromTestExtensionContext
toExtensionContext
.- 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 theExtensionContext
as well. - Ensure that the proper test instance is supplied to
TestTemplateContainerExtensionContext
extensions via theExtensionContext
as well.
- Ensure that the proper test instance is supplied to container-level extensions via the
- 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 bestatic
. - Update existing JavaDoc which states that
@BeforeAll
and@AfterAll
methods must bestatic
. - Update the release notes in the User Guide describing the new features.
- Update the release notes in the User Guide describing breaking changes.