I am writing some junit tests in eclipse and I need to do some time consuming setup before the tests. Appeared that @BeforeClass should be the way to do this. I currently tested this on a class that has 2 @Test functions.
When I right click on a class in eclipse and chose “Run As” -> “JUnit Test” I can see that the @BeforeClass is executed before both functions.
I even tried to change @BeforeClass to @Before and stored in a boolean whether we had already executed this function, but it seems that eclipse created two class objects from the same class, one for each test to run so that did not help either.
So what should I do to have a setup function run only one time even if I have many tests ? Or am I just using eclipse incorrectly when trying to run the tests ?
The setup is something like this:
public class SuperClass {
@BeforeClass
public void { // do timeconsuming setup }
}
public class TestClass extends SuperClass {
@Test
public void test1() { // perform first test }
@Test
public void test2() { // perform second test }
}
Making static the method annotated with BeforeClass may be the solution: