Search code examples
javatestcasejunit5test-suite

@Before and @After not working with JUnit 5 on Eclipse 2018-12 JAVA


I just created a test class from File->New->JUnit Test Cases and this is my whole code:

import static org.junit.jupiter.api.Assertions.*;
import org.junit.After;
import org.junit.Before;
import org.junit.jupiter.api.Test;

class TestingJUnit {

@Before
public void testOpenBrowser() {
    System.out.println("Opening Chrome browser");
}

@Test
public void tesingNavigation() {
    System.out.println("Opening website");
}

@Test
public void testLoginDetails() {
    System.out.println("Enter Login details");
}

@After
public void testClosingBrowser() {
    System.out.println("Closing Google Chrome browser");
}
}

But when I run this cas only @Test annotations are running @Before and @After annotations code are not running. Please guide me I think this is JUnit version or eclipse IDE version problem, and also my all Test classes not running in TestSuit I don't know why although Test Classes are running fine individually. My TestSuit Class is here:

import org.junit.runner.RunWith;
import org.junit.runners.Suite;
import org.junit.runners.Suite.SuiteClasses;

@RunWith(Suite.class)
@SuiteClasses({TestingJUnit.class, SecondTest.class})
public class TestSuit {

}

Solution

  • Try using @BeforeEach instead of @Before and @AfterEach instead of @After