Skip to content

@BeforeAll methods with same signature in superclass and subclass are called in JUnit 5.11, but not in JUnit 5.10 #4052

@tomtongue

Description

@tomtongue

After upgrading to JUnit 5.11, if a testbase class has a static method with the @BeforeAll annotation and its inherited testbase class also has the static method (with the same method name as its parent class) with the @BeforeAll annotation, both of the static methods with the @BeforeAll annotation are called. However, in Junit 5.10, only the 2nd static method with @BeforeAll is called.

I would like to ask if it's intended, and if it's intended, is there any way to suppress calling the first static method with the @BeforeAll annotation?

Steps to reproduce

Assuming the following class structure, the actual tests are in the TestA class.

abstract class TestBase (including @BeforeAll)
    <- abstract class Feature1TestBase (including @BeforeAll)
        <-  class TestA

When you run the tests in TestA,

  • In JUnit 5.11.1, a static method with @BeforeAll in the TestBase class is called, then the same name static method with @BeforeAll in the Feature1TestBase class is called.
  • In JUnit 5.10, only a static method with @BeforeAll in the Feature1TestBase class is called, the static method in the TestBase class is NOT called.

The below shows more details:

Code

TestBase:

// TestBase
import org.junit.jupiter.api.BeforeAll;

public abstract class TestBase {
    @BeforeAll
    public static void beforeAll() {
        System.out.println("Run beforeAll in TestBase.");
    }

    public void testMethod() {
        System.out.println("Run testMethod in TestBase.");
    }
}

Feature1TestBase:

import org.junit.jupiter.api.BeforeAll;

public abstract class Feature1TestBase extends TestBase {
    @BeforeAll
    public static void beforeAll() {
        System.out.println("Run beforeAll in Feature1TestBase.");
    }
}

TestA:

import org.junit.jupiter.api.Test;

public class TestA extends ConcreteTestBase {
    @Test
    public void runTestA1() {
        testMethod();
    }
}

Output

JUnit 5.11.1:

Run beforeAll in TestBase.
Run beforeAll in Feature1TestBase.
Run testMethod in TestBase.

JUnit 5.10.5:

Run beforeAll in Feature1TestBase.
Run testMethod in TestBase.

Context

  • Used versions (Jupiter/Vintage/Platform): JUnit 5.11.1 and JUnit 5.10.5 (org.junit.jupiter:junit-jupiter:5.11.1 and org.junit.jupiter:junit-jupiter:5.10.5)
  • Build Tool/IDE: IntelliJ IDEA 2024.1.4 (Ultimate Edition)

Deliverables

n/a

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions