Skip to content

Private lifecycle methods should cause an exception #2914

@juriad

Description

@juriad

The JavaDoc contains the following sentences:

@BeforeEach methods must have a void return type, must not be private, and must not be static. @BeforeEach methods are inherited from superclasses as long as they are not overridden. Furthermore, @BeforeEach methods from superclasses will be executed before @BeforeEach methods in subclasses.

Private methods are executed, even though they should not be (?). The JavaDoc prohibits private methods but does not explain what will happen.

Java defines overriding methods as: https://docs.oracle.com/javase/specs/jls/se8/html/jls-8.html#jls-8.4.8.1, the JavaDoc uses the term "overridden" with a different meaning than Java. JLS even says:

Note that methods are overridden or hidden on a signature-by-signature basis. If, for example, a class declares two public methods with the same name (§8.4.9), and a subclass overrides one of them, the subclass still inherits the other method.

In the previous issue #2390, it was suggested to also include mention of shadowing - which I think is also wrong. JLS already defines shadowing - https://docs.oracle.com/javase/specs/jls/se8/html/jls-6.html#jls-6.4.1.

Both instances should be either fixed to be compliant with the documentation, or the documentation should be updated.

Other annotations (@BeforeAll, @AfterAll, and @AfterEach) are expected to be affected too.

Steps to reproduce

Let's imagine two packages and a class in each:

package sup;

public class Sup {
    @BeforeEach
    MODSUP void before() {
        System.out.println("Before in Sup");
    }
}

package sub;
import sup.Sup;

public class Sub extends Sup {
    @BeforeEach
    void before() {
        System.out.println("Before in Sub");
    }

    @Test
    MODSUB void test() {
        System.out.println("Test");
    }
}

We can then write a table of expected behavior based on the modifiers:

Sub \ Sup public protected none private
public Before in SubTest Before in SubTest Before in SupBefore in SubTest (*) Before in SubTest
protected compilation error Before in SubTest Before in SupBefore in SubTest (*) Before in SubTest
none compilation error compilation error Before in SupBefore in SubTest (*) Before in SubTest
private compilation error compilation error Before in SupTest (*) Test (*)

The combinations with (*) instead produce:

Before in Sub
Test

Context

  • Used versions (Jupiter/Vintage/Platform): 5.8.2
  • Build Tool/IDE: Gradle, Java 11

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions