Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
tomtongue opened this issue Oct 6, 2024 · 4 comments

Comments

@tomtongue
Copy link

tomtongue commented Oct 6, 2024

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

@tomtongue tomtongue changed the title Both @BeforeAlls with the same static method name in a parent class and its inherited class are called in JUnit 5.11, but not in JUnit 5.10 Both @BeforeAlls with the same static method names in a parent class and its inherited class are called in JUnit 5.11, but not in JUnit 5.10 Oct 6, 2024
@sbrannen sbrannen self-assigned this Oct 6, 2024
@sbrannen
Copy link
Member

sbrannen commented Oct 6, 2024

Hi @tomtongue,

Sorry to ask the obvious, but did you read the release notes for JUnit 5.11 before posting this question?

I would like to ask if it's intended

This is intentional, and it's very well documented.

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

Yes, you can revert to "legacy" mode, which is also documented in the User Guide.

In light of the above, I am closing this issue.

Regards,

Sam

@sbrannen sbrannen closed this as not planned Won't fix, can't repro, duplicate, stale Oct 6, 2024
@sbrannen sbrannen changed the title Both @BeforeAlls with the same static method names in a parent class and its inherited class are called in JUnit 5.11, but not in JUnit 5.10 @BeforeAll methods with same signature in superclass and subclass are called in JUnit 5.11, but not in JUnit 5.10 Oct 6, 2024
@tomtongue
Copy link
Author

@sbrannen Thanks so much for pointing these documents. Just one thing, I set the legacy semantics to true, but it seems not to work for those static methods. https://github.com/junit-team/junit5/blob/main/junit-platform-commons/src/main/java/org/junit/platform/commons/util/ReflectionUtils.java this code only has the legacy mode only for fields. Please correct me if I'm wrong.

@sbrannen
Copy link
Member

sbrannen commented Oct 6, 2024

@sbrannen Thanks so much for pointing these documents.

You're welcome.

Just one thing, I set the legacy semantics to true, but it seems not to work for those static methods. main/junit-platform-commons/src/main/java/org/junit/platform/commons/util/ReflectionUtils.java this code only has the legacy mode only for fields. Please correct me if I'm wrong.

The useLegacySearchSemantics flag is honored in two places, for fields and methods. Please search for use of that flag for details.

@tomtongue
Copy link
Author

tomtongue commented Oct 6, 2024

Thank you for sharing the flag, sorry I missed that. Thanks so much, let me check those documents again.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants