简体   繁体   中英

@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 {

}

尝试使用@BeforeEach代替@Before@AfterEach代替@After

Try Using This

@BeforeEach
public void before() {
    System.out.println("Before");
}

@AfterEach
public void after() {
    System.out.println("After");
}

@Test
public void sum_with3numbers() {
    System.out.println("Test 1");
}

@Test
public void sum_with1numbers() {
    System.out.println("Test 2");
}

Output will be:

Before
Test 1
After
Before
Test 2
After

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM