I am using JUnit 4 to test a backend system with an in-memory database. I am using @BeforeClass @Before @After and @AfterClass.
It is working great so far on the class level.
@BeforeClass contains the database setup which is slow but only needs to be done once per test session.
@Before just wipes the slate clean for the next test to run. It's pretty fast.
My tests looks something like this :
class CompanyTest {
@BeforeClass
public static void beforeAll() throws Exception {
BeforeAll.run(); //Setup In Memory Database!!! Very time intensive!!!
}
@Before
public void beforeTest() throws Exception {
BeforeTest.run(); //Setup data in database. Relatively quick
}
@Test
public void testCreateCompany() throws Exception {
///...
}
@Test
public void testDeleteCompany() throws Exception {
///...
}
@Test
public void testAdminCompany() throws Exception {
///...
}
@After
public void afterTest() {
AfterTest.run(); //clear data
}
@AfterClass
public static void afterAll() throws Exception {
AfterAll.run(); //tear down database
}
}
It is working great so far on the class level.
I can right click (in Eclipse) on an individual test and it will run @BeforeClass and then @Before.
I can also click (in Eclipse) on the class itself, and it will run @BeforeClass only once and then @Before before every test.
....but how is this principle extended to the Suite level?
I want to run @BeforeClass before all my classes in my Suite. If I write my Suite like this :
@Suite.SuiteClasses({ CompanyTest.class, CustomerTest.class, SomeOtherTest.class, })
public class AllTests {
@BeforeClass
public static void beforeAll() throws Exception {
BeforeAll.run();
}
@AfterClass
public static void afterAll() throws Exception {
AfterAll.run();
}
}
... I need to remove @BeforeClass from all my test classes. This is annoying because I have a lot of test classes, and i don't want to delete my @BeforeClass because I want to test them indicidually.
What I am basically saying is :
Is there an easy way (click of a mouse in IDE) to test JUnit tests at the (a) method level, (b) class level, and (c) suite level, while maintaining a session level setUp and tearDown process?