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.
@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.
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.
@BeforeClass: The @BeforeClass annotated method runs before the execution of test methods in a current 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.
You're shadowing the abstract class's static method; name one of them something different.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With