-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
@TempDirfields with the same name in a superclass and subclass would both be injected. - Two
@BeforeEachmethods with the same name/signature in a superclass and subclass would both be invoked, if the@BeforeEachmethod in the superclass is package-private and the subclass resides in a different package (i.e., the@BeforeEachmethod in the subclass does not@Overridethe@BeforeEachmethod 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
@BeforeAllmethod in super class skipped when it has same name as a@BeforeEachmethod in subclass #3498@TempDirfield in super class skipped when it has same name as a@TempDirfield 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.