Fundamentals 11 min read

JUnit Annotations for Selenium Testing: Overview and Usage

This article explains why JUnit is a popular Java testing framework, lists its key features, and provides a comprehensive guide to using JUnit annotations—including @BeforeClass, @Before, @Test, @After, @AfterClass, @Ignore, and advanced annotations—in Selenium WebDriver automation scripts, complete with code examples and execution order details.

FunTester
FunTester
FunTester
JUnit Annotations for Selenium Testing: Overview and Usage

Why JUnit Is Popular Among Testers

JUnit is a leading open‑source Java testing framework that offers IDE integration (Eclipse, IntelliJ), CI/CD tool support (Jenkins, TeamCity), rich assertion capabilities, a variety of annotations, test suite creation tools, test runners, more readable test code, and HTML test report generation.

What Are JUnit Annotations

JUnit annotations help identify the type of methods in Selenium WebDriver test scripts, allowing developers to set up, execute, and clean up test cases effectively.

@BeforeClass

Executed once before any test methods in the class; used for static initialization of objects needed for all tests.

@BeforeClass
public static void SetUpClass() {
    System.out.println("This is @BeforeClass annotation");
}

@Before

Runs before each test method; typically used to set up the test environment for every individual test case.

@Before
public void SetUp() {
    System.out.println("This is @Before annotation");
}

@Test

Marks a public void method as a test case that JUnit will execute. Multiple @Test methods can exist within a single test class.

@Test
public void Addition() {
    c = a + b;
    assertEquals(15, c);
    System.out.println("This is first @Test annotation method= " + c);
}

@Test
public void Multiplication() {
    c = a * b;
    assertEquals(50, c);
    System.out.println("This is second @Test annotation method= " + c);
}

@After

Runs after each test method to release resources or clean up temporary data created in @Before.

@After
public void TearDown() {
    c = null;
    System.out.println("This is @After annotation");
}

@AfterClass

Executed once after all test methods have finished; used to clean up static resources initialized in @BeforeClass.

@AfterClass
public static void TearDownClass() {
    System.out.println("This is @AfterClass annotation");
}

@Ignore

Indicates that a test method should be skipped, useful when a test is not ready or would cause failures.

@Ignore
public void IgnoreMessage() {
    String info = "JUnit Annotation Blog";
    assertEquals(info, "JUnit Annotation Blog");
    System.out.println("This is @Ignore annotation");
}

Execution Order of JUnit Annotations

The typical execution flow is: @BeforeClass → @Before → @Test → @After → (repeat for each test) → @AfterClass.

Advanced JUnit Annotations for Specific Purposes

@ParameterizedTest

Runs the same test method multiple times with different parameters supplied by @ValueSource or other providers.

@ParameterizedTest
@ValueSource(strings = {"LambdaTest", "JUnit", "Annotations", "Blog"})
void ExampleCode(String data) {
    assertNotNull(data);
}

@RepeatedTest

Executes a test method repeatedly a specified number of times.

@Test
@RepeatedTest(5)
public void Addition() {
    int a = 10;
    int b = 5;
    Object c = a + b;
    assertEquals(15, c);
    System.out.println("This is @RepeatedTest annotation method= " + c);
}

@RunWith

Specifies a custom runner (e.g., Suite) to execute a collection of test classes.

@RunWith(Suite.class)
@Suite.SuiteClasses({TestSample1.class, TestSample2.class, TestSample3.class, TestSample4.class, TestSample5.class})
public class JunitTest {
}

@Parameters

Used with @RunWith(Parameterized.class) to supply a list of parameter sets for a test class.

@RunWith(Parameterized.class)
public class Blog1 {
    String name, password;

    @Parameters
    public Object[][] getData() {
        return new Object[][]{{"ramit1", "ramitd11"}, {"ramit2", "ramitd22"}};
    }

    public Blog1(String id, String pass) {
        this.name = id;
        this.password = pass;
    }

    @Test
    public void Sample() {
        SampleCode.login(name, password);
    }
}

Timeout Attribute

Specifies a maximum execution time for a test method in milliseconds.

@Test(timeout = 10000)
public void ExampleCode() throws InterruptedException {
    System.setProperty("webdriver.chrome.driver", "chromedriver.exe");
    WebDriver driver = new ChromeDriver();
    driver.get("https://www.example.com");
    driver.manage().window().maximize();
    Thread.sleep(2000);
    driver.quit();
}

@Rule for Global Timeout

Defines a global timeout applied to all @Test methods in the class.

@Rule
public Timeout globalTimeout = Timeout.seconds(10); // 10 seconds

expected Attribute

Specifies the exception type that a test method is expected to throw.

@Test(expected = ArithmeticException.class)
public void ExampleCode() {
    int a = 10, b = 0, c;
    c = a / b; // will throw ArithmeticException
    System.out.println("Value= " + c);
}

The article also includes a flowchart image illustrating the annotation execution sequence and a list of related testing topics.

JavaAutomationTestingAnnotationsJunitSelenium
FunTester
Written by

FunTester

10k followers, 1k articles | completely useless

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.