Introduction to JUnit Framework and Its Usage in Selenium Script – Selenium Tutorial #11

This tutorial will give an insight about JUnit and its usage in selenium script. This is tutorial #11 in our comprehensive Selenium tutorials series.

Basically, JUnit is an open source unit testing tool and used to test small/large units of code. To run the JUnit test you don’t have to create a class object or define the main method. JUnit provides assertion library which is used to evaluate the test result. Annotations of JUnit are used to run the test method. JUnit is also used to run the Automation suite having multiple test cases.

What You Will Learn:

Adding JUnit library in Java project

First, we will learn how to add JUnit library in your Java project:

Step #1: Right click on Java project->Build Path->Configure Build path

Step #2: Click Libraries->Add Library

Step #3: Click on Junit.

Step #4: Select Junit4->Finish

Step #5: Click OK.

There are many frameworks like Data Driven Framework, Keyword Driven Framework, and Hybrid Framework which use JUnit tool as test runner and which will help to start the batch execution and reporting.

JUnit Annotations Used in Selenium scripts

There are many annotations available in Junit. Here we have described few annotations which are used very frequently in Selenium scripts and framework.

#1. @Test

@Test annotation is used to run a Junit test.

Example:

 @Test
public void junitTest()
{
System.out.println("Running Junit test");
Assert.assertEquals(1,1);
} 

How to Run a JUnit test:

Navigate to run ->Run as JUnit test

#2. @Before:

@Before annotation is used to run any specific test before each test.

 public class Junttest {
@Before
public void beforeTest(){
System.out.println("Running before test");
}

@Test
public void junitTest(){
System.out.println("Running Junit test");
}
} 

Output:

Running before test

Running Junit test

Example of before annotation using two junit test method.

 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");
}
} 

Output:

Running before test

Running JUnit test

Running before test

Running second JUnit test

Before running JUnit test method beforeTest method will run. Similarly, before running secondJuntiTest again beforeTest method will run and produces output like above.

#3. @BeforeClass

This method executes once before running all test. The method has to be a static method. Initialization of properties files, databases etc are done in the beforeClass method.



 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");
}
} 

Output:

Executed before class method

Running JUnit test

Running second JUnit test

#4. @After

This method executes after each test.

 public class Junttest {
@Test
public void junitTest(){
System.out.println("Running Junit test");
}

@After
public void afterTest(){
System.out.println("Running after method");
}
} 

Output:

Running JUnit test

Running after method

#5. @AfterClass

Like @BeforeClass, @AfterClass executes once after executing all test methods. Like a @BeforeClass method, @AfterClass method has to be a static method.

 public class Junttest {

@Test
public void junitTest(){
System.out.println("Running Junit test");
}

@Test
public void secondJunitTest(){
System.out.println("Running second Junit test");
}

@AfterClass
Public static void afterClassTest(){
System.out.println("Running afterclass method");
}
} 

Output:

Running JUnit test

Running second JUnit test

Running afterclass method

JUnit assertions are used to validate certain condition and stops the execution of the program if the conditions are not satisfied.

#6. Parameterized JUnit class:

A parameterized class is used to run the same scenario with multiple datasets.

Below is the example to pass multiple parameters in a JUnit test.

@Parameters annotation tag is used to pass multiple data. Here, we have taken 2*2 dimensional array and the data can be visualized like below:

 @RunWith(Parameterized.class)
public class Junttest {
public String name;
public int age;
public Junttest(String name,int age){
this.name=name;
this.age=age;
}

@Test
public void testMethod(){
System.out.println("Name is: "+name +" and age is: "+age);
}

@Parameters
public static Collection<Object[]> parameter(){
Object[][] pData=new Object[2][2];
pData[0][0]="Tom";
pData[0][1]=30;
pData[1][0]="Harry";
pData[1][1]=40;
return Arrays.asList(pData);
}
} 

JUnit Assertions

JUnit assertEquals: This checks if the two values are equal and assertion fails if both values are not equal.

This compares Boolean, int, String, float, long, char etc.

Syntax:

Assert.assertEqual(“excepted value”, ”actual value”);

Example:

Assert.assertEqual(“ABC”,”ABC”); //Both the strings are equal and assertion will pass.

Assert.assertEqual(“ABC”,”DEF”); //Assertion will fail as both the strings are not equal.

Assert.assertEqual(“Strings are not equal”, “ABC”,”DEF”); //message will be thrown if the equal condition is not satisfied.

Below is the example of the use of JUnit assertion in selenium:

 String username=driver.findElement(By.id(“username”)).getText();
String password=driver.findElement(By.id(“password”)).getText();
Assert.assertEqual(“Mismatch in both the string”, username, password); 

In above example assertion will fail as both the strings are not equal. One is the text of username field and other is the text of password field.

JUnit assertTrue: Returns true if the condition is true and assertion fails if the condition is false.

Assert.assertTrue(“message”, condition);

Assert.assertTrue(“Both the strings are not equal”, (“HelloWorld”).equals(“HelloWorld”));

Here assertion will pass as both the strings match. It will print the message if the assertion fails.

JUnit assertFalse: Returns true if the condition is false and assertion fails if the condition is true.

Assert.assertFalse(“message”, condition);

Assert.assertFalse(“Both the strings are equal”, (“Hello”).equals(“HelloWorld”));

There will not be any assertion error as the condition is false.

Conclusion:

Most of the programmers use Junit as it is easy and does not take much effort to test. A simple green or red bar will show the actual result of the test. Junit makes life easy as it has its own set of libraries and annotations. Here we have also described commonly used annotations used with selenium scripts and framework.

More detail about the framework and use of JUnit annotations will be discussed in the upcoming tutorial which is dedicated exclusively for framework design using Junit. This tutorial will help us in designing the framework using Junit.

Next Tutorial #12: In next tutorial, we would discuss all about TestNG, its features and its applications. TestNG is an advance framework designed in a way to leverage the benefits by both the developers and testers.