I marked a method with jUnit\'s @BeforeClass annotation, and got this exception saying it must be static. What\'s the rationale? This forces all my init to be on static fiel
It seems that JUnit creates a new instance of the test class for each test method. Try this code out
public class TestJunit
{
int count = 0;
@Test
public void testInc1(){
System.out.println(count++);
}
@Test
public void testInc2(){
System.out.println(count++);
}
@Test
public void testInc3(){
System.out.println(count++);
}
}
The output is 0 0 0
This means that if the @BeforeClass method is not static then it will have to be executed before each test method and there would be no way to differentiate between the semantics of @Before and @BeforeClass