Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

@BeforeClass method of parent class is not invoked

Tags:

java

junit

According to this document @BeforeClass methods of superclasses will be run before those the current class. But it doesn't happen in my case.

I'm using junit 4.8.1.

Could you please tell me what I'm doing incorrectly ?

My parent class looks like this:

public abstract class AbstractPromoterUnitTest extends TestCase {
    @BeforeClass
    public static void setUpOnce() {
        // Do something here.
    }
}

It's child:

@RunWith(JUnit4.class)
public abstract class NormalPromoterUnitTest extends AbstractPromoterUnitTest{
    @BeforeClass
    public static void setUpOnce() {
        // Do something here 2.
    }
}

NormalPromoterUnitTest.setUpOnce() is called. AbstractPromoterUnitTest.setUpOnce() is not.

like image 704
expert Avatar asked Aug 30 '12 23:08

expert


People also ask

What is the difference between @before and @BeforeClass annotation?

@BeforeClass will be run before the entire test suits whereas @Before will be run is executed before each test, while @BeforeClass runs once before the entire test fixture. If your test class has ten tests, @Before code will be executed ten times, but @BeforeClass will be executed only once.

What is @BeforeClass in Java?

Annotating a public static void no-arg method with @BeforeClass causes it to be run once before any of the test methods in the class. The @BeforeClass methods of superclasses will be run before those of the current class, unless they are shadowed in the current class.

What is @BeforeClass in Testng?

@BeforeClass: The @BeforeClass annotated method runs before the execution of test methods in a current class.

Which of the following annotation is used for actions to be performed before each test class?

Methods annotated with the @Before annotation are run before each test. This is useful when we want to execute some common code before running a test. Notice that we also added another method annotated with @After in order to clear the list after the execution of each test.


1 Answers

You're shadowing the abstract class's static method; name one of them something different.

like image 57
Dave Newton Avatar answered Sep 21 '22 04:09

Dave Newton