Skip to content

Align method and field search algorithms with Java semantics #3553

Closed
@sbrannen

Description

@sbrannen

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

Related Issues

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.

Metadata

Metadata

Assignees

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions