Skip to content

@BeforeAll method in super class skipped when it has same name as a @BeforeEach method in subclass #3498

Closed

Description

Steps to reproduce

Given:

package a;

import org.junit.jupiter.api.BeforeAll;

public abstract class A {

    @BeforeAll
    static void before() {
        System.out.println("A");
    }
}

and

package b;

import a.A;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

public class B extends A {

    @BeforeEach
    void before() {
        System.out.println("B");
    }

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

Executing the test in B prints: B and Hi there. Note the absence of A: the @BeforeAll method was not called. Note that the non-static before method in B does not override the static method (it is in a different package), nor does it conflict with it. Renaming the before method in B or A fixes the problem.

This can easily happen when you have an abstract base class in some utility package, and you are extending it to build tests. If you happen to name a method in this test class that conflicts with a package-private method in the base class, things do not work as expected.

Workaround:

Ensure the names in the base class are sufficiently unique that they're unlikely to have similar names in a subclass.

Deliverables

  • A fix for this problem. It probably is a problem with annotation scanning not being thorough enough and not taking this special case into account.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions