A method annotated with @BeforeClass means that it runs once before any of the test methods runs in the test class. The method annotated with @Before is run once before each testing method in the class. For them are @AfterClass and @After .
Perhaps you are aiming for something like the following.
@BeforeClass public static void setUpClass() { // Initialize stuff once for ALL tests (run once) } @Before public void setUp() { // Initialize stuff before every test (this is run twice in this example) } @Test public void test1() { /* Do assertions etc. */ } @Test public void test2() { /* Do assertions etc. */ } @AfterClass public static void tearDownClass() { // Do something after ALL tests have been run (run once) } @After public void tearDown() { // Do something after each test (run twice in this example) }
You do not need to explicitly call the @BeforeClass method in your testing methods, JUnit does this for you.
ponzao
source share