Bekind
Bekind

Reputation: 55

Why is my Selenium JUnit test bypassing @Before & @After annotations?

I have written some code to print out the words "Setting up " and then run the test and then print out "Closing the test". But Selenium is skipping my [Before] and [After] methods and only running the test by itself.

package smoketests;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.jupiter.api.Test;
import org.openqa.selenium.WebDriver;

public class PageTitleJUnit {

    @Test
    public void PageTitleTest() {
        System.out.println("Running the test");
        String webURL = "http://sdettraining.com/trguitransactions/AccountManagement.aspx";

        WebDriver driver = utilities.DriverFactory.open("Chrome");
        driver.get(webURL);
        String actualTitle = driver.getTitle();
        String expectedTitle = "SDET Training | Account Management";

        Assert.assertEquals(expectedTitle, actualTitle);            
    }

    @Before
    public void setUp() {
        System.out.println("Setting up");   
    }
    @After
    public void tearDown() {
        System.out.println("Closing the test");
    }

}

For some reason it's only printing out "Running the test" from the @Test Method and it's skipping my Before and After methods. I tried re-writing the code, and restarting eclipse. But nothing's helped so far. Here's a screenshot of the console log.Eclipse Console Screenshot

Upvotes: 1

Views: 1363

Answers (1)

undetected Selenium
undetected Selenium

Reputation: 193098

The main issue is you have mixed up both the JUnit 4.x annotations and JUnit 5.x annotations.

The basic annotations of JUnit 4.x are as follows :

  • @BeforeClass
  • @AfterClass
  • @Before
  • @After
  • @Test

You have resolved @Before and @After annotations respectively through import org.junit.Before; and import org.junit.After;

But you have referenced @Test through import org.junit.jupiter.api.Test;

Solution

  • Remove the references of JUnit 5.x external jars from your Project Workspace.
  • Resolve @Test annotation through import org.junit.Test.
  • Clean the Project Workspace through your IDE.
  • Execute your Tests.

Upvotes: 3

Related Questions