Basic Java JUnit Test Setup

Here is a sample WebDriver Java test using the JUnit framework:

package WebDriverTest1;

import static org.junit.Assert.*;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class WebDriverTest1
  private WebDriver driver;

  public void setup() throws Exception
    driver = new FirefoxDriver();

  public void teardown() throws Exception

  public void WebDriverTips()
    String title = driver.getTitle();
    assertEquals("Title mismatch", "WebDriver Tips", title);

As we can see from my earlier C# Example, there isn’t a world of difference between the two. There is no corresponding Test Fixture attribute but there are attributes for Before, After and Test. Similar to NUnit, the Before method gets executed before every test case, and the After method gets executed after every test case. Each test case is indicated with the @Test annotation.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.