Description
Overview
As discussed in #3532 (comment), the search algorithms in JUnit 5 that are used to locate methods and fields have historically had the following undesirable characteristic.
A field supersedes (shadows, hides, overrides) a field with the same name in a superclass unless a filter (Predicate
) is applied that ignores one of the fields (such as a static
check or a check for the presence of a specific annotation).
This also applies to any methods looked up via JUnit 5's reflection support -- for example, lifecycle methods and test methods in JUnit Jupiter.
The JUnit 5 team discussed this topic today and decided to revise our search algorithms to align with Java semantics regarding visibility and overriding.
Specifically, we want to ensure that our search algorithms do not treat something as overridden when it is not overridden according to the Java Virtual Machine Specification (JVMS).
As a consequence, several currently unsupported use cases would become supported -- for example:
- Two
@TempDir
fields with the same name in a superclass and subclass would both be injected. - Two
@BeforeEach
methods with the same name/signature in a superclass and subclass would both be invoked, if the@BeforeEach
method in the superclass is package-private and the subclass resides in a different package (i.e., the@BeforeEach
method in the subclass does not@Override
the@BeforeEach
method in the superclass according to the JVMS).
External Resources
- Java Virtual Machine Specification: § 5.4.5, Overriding
Related Issues
- Detect annotations on overridden super type methods #960
- JUnit 5 computes overrides relationships incorrectly #2390
- Document best practices for test method and test class visibility #2626
- Revert disallowing private lifecycle methods #3242
@BeforeAll
method in super class skipped when it has same name as a@BeforeEach
method in subclass #3498@TempDir
field in super class skipped when it has same name as a@TempDir
field in subclass #3532
Deliverables
- Align method and field search algorithms with Java semantics regarding visibility/overridability.
- Introduce tests for overridden method support. See comment below.
- Introduce a configuration parameter / system property -- for example,
junit.platform.reflection.search.useLegacySemantics = true
-- that allows a project to revert to the previous behavior. - Document in User Guide.
- Document in Release Notes.