1st solution
Use TestNG instead.
@Before*
annotations behave this way in TestNG.
No method annotated with @Before*
has to be static.
@org.testng.annotations.BeforeClass
public void setUpOnce() {
//I'm not static!
}
2nd solution
And if you don't want to do that, you can use an execution listener from Spring (AbstractTestExecutionListener
).
You will have to annotate your test class like this:
@TestExecutionListeners({CustomTestExecutionListener.class})
public class Test {
//Some methods with @Test annotation.
}
And then implement CustomTestExecutionListener
with this method:
public void beforeTestClass(TestContext testContext) throws Exception {
//Your before goes here.
}
Self-contained in one file that would look like:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"commonContext.xml" })
@TestExecutionListeners({SimpleTest.class})
public class SimpleTest extends AbstractTestExecutionListener {
@Override
public void beforeTestClass(TestContext testContext) {
System.out.println("In beforeTestClass.");
}
@Test
public void test() {
System.out.println("In test.");
}
}