JUnit Annotations in webdriver; An Overview

JUnit, the unit test framework for java programming languages play an important role in test driven development process. It can be incorporated with selenium to enhance the performance of selenium. However, a few JUnit annotations should be added to selenium webdriver if you want to run test scripts using JUnit framework.

The most commonly used JUnit annotations in webdriver are

  • @Test
  • @Before
  • @After
  • @Before class
  • @After class

@Test

All the methods mentioned under the annotation @Test will be executed using JUnit.

Example

@test

public void junitTest()

{

System.out.println(“Running Junit test”);

Assert.assertEquals(1,1);

}

@Before

All methods mentioned under this annotation will be executed first and then the other methods mentioned under @Test.

Example

public class Junttest

{

@Before

public void beforeTest()

{

System.out.println(“Running before test”);

}

@Test

public void junitTest()

{

System.out.println(“Running Junit test”);

}

}

@After

All the methods mentioned under this annotation will executed after finishing the execution of methods mentioned under @Test.

Example

public class Junttest

{

@Before

public void beforeTest()

{

System.out.println(“Running before test”);

}

@Test

public void junitTest()

{

System.out.println(“Running Junit test”);

}

@Test

public void secondJunitTest()

{

System.out.println(“Running second Junit test”);

}

}

@Before Class

Methods mentioned in the class specified by this annotation will be executed first and after that the methods mentioned under @Test will be executed. This has to be a static method.

Example

public class Junttest

{

@BeforeClass

public static void beforeClassTest()

{

System.out.println(“Executed before class method”);

}

@Test

public void junitTest()

{

System.out.println(“Running Junit test”);

}

@Test

public void secondJunitTest()

{

System.out.println(“Running second Junit test”);

}

}

@After Class

Methods in the class specified by this annotation will be executed after completing the execution of methods mentioned under the annotation @Test. This also should be a static method.

Example

public class Junttest

{

@Test

public void junitTest()

{

System.out.println(“Running Junit test”);

}

@After

public void afterTest()

{

System.out.println(“Running after method”);

}

Be First to Comment

Leave a Reply

Your email address will not be published. Required fields are marked *