It works for me
Junit 3
public abstract class BaseTest extends TestCase { public void setUp(){ System.out.println("before called"); } } public class Test1 extends BaseTest { public void test() { Assert.assertTrue(true); System.out.println("Test1"); } } public class Test2 extends BaseTest { public void test() { Assert.assertTrue(true); System.out.println("Test1"); } }
The output I get is
before called Test2 before called Test1
Junit 4
For JUnit4, you donβt even need to abstract from the base class. You can simply use the following
public class BaseTest { @Before public void setUp(){ System.out.println("before called"); } } public class Test1 extends BaseTest { @Test public void test() { Assert.assertTrue(true); System.out.println("Test1"); } } public class Test2 extends BaseTest { @Test public void test() { Assert.assertTrue(true); System.out.println("Test1"); } }
I would highly recommend using JUnit 4. Using annotations means you break some of these inheritance dependencies, which can be confusing.
source share