Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

}
like image 358
Aashir Haque Avatar asked Jan 25 '19 13:01

Aashir Haque


People also ask

Can JUnit 4 and 5 be used together?

You can migrate tests written in JUnit 4 to JUnit 5 with minimal effort. Here are the steps to perform the migration: The JUnit Vintage engine in JUnit 5 helps in running existing test cases written in JUnit 4 (or JUnit 3) utilizing the JUnit Platform.

Does JUnit 5 work with Java 11?

To use JUnit5 in an Maven project, you need to: Configure to use Java 11 or higher, as this is required by JUnit5.

What is @before and @after in JUnit?

@Before : The @Before methods of superclasses will be run before those of the current class, unless they are overridden in the current class. No other ordering is defined. @After : The @After methods declared in superclasses will be run after those of the current class, unless they are overridden in the current class.

Does Eclipse support JUnit 5?

To give JUnit 5 a spin, you have the tooling support in the Eclipse IDE ready at your disposal. Download Eclipse Oxygen.


1 Answers

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

like image 88
Ros5292 Avatar answered Oct 12 '22 14:10

Ros5292