question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

@BeforeClass of superclass is not executed

See original GitHub issue

Contrary to the statement in @BeforeClass annotation javadoc : /** The @BeforeClass methods of superclasses will be run before those the current class. */ The @BeforeClass annotated method of superclass is not called if there is another @BeforeClass is subclass.

public abstract class SuperTest {
    @BeforeClass
    public static void beforeClass() throws Exception {
        fail("why don't you call me ?");    // never fails
    }
}

public class MyTestA extends SuperTest {
    @BeforeClass
    public static void beforeClass() {
        fail("Thanks for calling");     // fails here
    }

    @Test
    public void test() {
        fail("Failure expected from MyTestA.beforeClass()");  
    }
}

public class MyTestB extends SuperTest {
    @BeforeClass
    public static void beforeClass() {
    }

    @Test
    public void test() {
        fail("Failure expected from SuperTest.beforeClass()");    // fails here
    }
}

Issue Analytics

  • State:closed
  • Created 12 years ago
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

11reactions
taralcommented, Mar 6, 2012

The method in the superclass is shadowed by the method with the same name in the subclass. If you give the @BeforeClass methods different names, they will run in the correct order.

1reaction
gonzencommented, Mar 7, 2012

thanks taral, issue is settled then. A little mention in the javadoc of @BeforeClass could be helpful though.

Read more comments on GitHub >

github_iconTop Results From Across the Web

TestNG @BeforeClass doesn't run from super class if it not an ...
I have @BeforeClass testng annotation in class A. And I only annotate class C with @Test. Now, I expect that while running class...
Read more >
AfterClass (JUnit API)
The @AfterClass methods declared in superclasses will be run after those of the current class, unless they are shadowed in the current class....
Read more >
Before vs @BeforeClass vs @BeforeEach vs @BeforeAll
When we want to execute an expensive common operation before each test, it's preferable to execute it only once before running all tests...
Read more >
Common test superclass, @BeforeClass fails for one test, and ...
Hi Cedril, I have a test hierarchy: public class AbstractTest { @BeforeClass public void beforeAbstractTestClass() { System.out.println("<<<<<>>>>>
Read more >
JUnit Test Cases @Before @BeforeClass Annotation - Guru99
JUnit does not require server for testing web application, which makes the testing ... Execute the JUnit @Before methods in the superclass ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found