Skip to content

JUnit 5 computes overrides relationships incorrectly #2390

@tjwilson90

Description

@tjwilson90

junit-platform-commons:1.6.0

package foo;

import org.junit.jupiter.api.BeforeEach;

public class Foo {
    @BeforeEach
    final void beforeEach() {
        System.out.println("foo");
    }
}
package bar;

import foo.Foo;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

class Bar extends Foo {
    @BeforeEach
    final void beforeEach() {
        System.out.println("bar");
    }

    @Test
    final void test() { }
}

This outputs

bar

but should output

foo
bar

The reason it does not output foo is because ReflectionUtils.isMethodShadowedBy is used to compute overrides while scanning for all methods in a class hierarchy. That considers only the signature of the method which is not sufficient according to the language specification, https://docs.oracle.com/javase/specs/jls/se8/html/jls-8.html#jls-8.4.8.1

The reason why I care about this is because I want to declare a @BeforeEach method in a super class and always have it execute regardless of what subclasses are doing.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions