I have multiple JUnit test classes to run with multiple tests in each of them. I have a base test class, which is the parent for all test classes. Is there any way to write methods in base test class that executes before @BeforeAll
and after @AfterAll
only once for all test classes? Or is there any interface
that can be implemented or any class that can be extended to solve this problem?
BaseJUnitTest.class
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.platform.runner.JUnitPlatform;
import org.junit.runner.RunWith;
@RunWith(JUnitPlatform.class)
public class BaseJUnitTest {
// method to execute before all JUnit test classes
@BeforeAll
public static void beforeAll() throws Exception {
}
@AfterAll
public static void afterAll() throws Exception {
}
@BeforeEach
public void beaforeEachTest() {
}
@AfterEach
public void afterEachTest() {
}
// method to execute after all JUnit test classes
}
SampleTest.class
public class SampleTest extends BaseJUnitTest {
@Test
public void test1() {
System.out.println("SampleTest test1");
}
}
Note: I'm using JUnit 5, although in this scenario I hope it doesn't matter much about JUnit 4 or 5