In this lesson, we will provide a comprehensive summary of the JUnit and explain its application in a selenium script. This tutorial represents the 11th part of our exhaustive Selenium tutorials series.
At its core, JUnit is a free-of-charge unit testing tool that is employed to test small or extensive code units. In contrast to conventional testing methods, executing JUnit tests obviates the need for forming a class object or defining a main method. The JUnit framework offers an assertion library that gauges the test results. JUnit notations are utilized to carry out the test methods and can also be applied to execute automation suites incorporating multiple test cases.
Recommended IPTV Service Providers
- IPTVGREAT – Rating 4.8/5 ( 600+ Reviews )
- IPTVRESALE – Rating 5/5 ( 200+ Reviews )
- IPTVGANG – Rating 4.7/5 ( 1200+ Reviews )
- IPTVUNLOCK – Rating 5/5 ( 65 Reviews )
- IPTVFOLLOW -Rating 5/5 ( 48 Reviews )
- IPTVTOPS – Rating 5/5 ( 43 Reviews )
What You Will Learn:
Integrating JUnit Library into a Java Project
Firstly, let’s understand how to integrate the JUnit library into your Java project:
Step #1: Right-click on the Java project, pick “Build Path,” and then opt for “Configure Build Path.”
Step #2: Tap on the “Libraries” tab and choose “Add Library.”
Step #3: Select “JUnit.”
Step #4: Opt for JUnit4 and click on “Finish.”
Step #5: Click “OK.”
JUnit is frequently employed in frameworks such as Data Driven Framework, Keyword Driven Framework, and Hybrid Framework. These frameworks use JUnit as a test runner, permitting batch implementation and reporting.
JUnit Annotations Employed in Selenium Scripts
JUnit provides numerous annotations that are commonly used in Selenium scripts and frameworks. Below, we will outline a few of these annotations:
#1. @Test
The @Test annotation is employed to execute a JUnit test.
Example:
@Test public void junitTest() { System.out.println("Executing Junit test"); Assert.assertEquals(1,1); }
How to Execute a JUnit test:
Go to run ->Run as JUnit test
#2. @Before:
The @Before annotation is applied to execute a particular test before each test method.
public class Junttest { @Before public void beforeTest(){ System.out.println("Executing before test"); } @Test public void junitTest(){ System.out.println("Executing Junit test"); } }
Output:
Executing before test
Executing Junit test
Here, we present an example of the @Before annotation used with two JUnit test methods:
public class Junttest { @Before public void beforeTest(){ System.out.println("Executing before test"); } @Test public void junitTest(){ System.out.println("Executing Junit test"); } @Test public void secondJunitTest(){ System.out.println("Executing second Junit test"); } }
Output:
Executing before test
Executing JUnit test
Executing before test
Executing second JUnit test
The @Before method is performed prior to each JUnit test method. In the above example, the beforeTest method is executed before the junitTest and secondJunitTest methods.
#3. @BeforeClass
The @BeforeClass annotation is applied to perform a method only once before implementing all test methods in a class. The method needs to be labelled as static. This is typically employed for initializing properties files, databases, etc.
public class Junttest { @BeforeClass public static void beforeClassTest(){ System.out.println("Executed method before class"); } @Test public void junitTest(){ System.out.println("Executing Junit test"); } @Test public void secondJunitTest(){ System.out.println("Executing second Junit test"); } }
Output:
Executed method before class
Executing JUnit test
Executing second JUnit test
#4. @After
The @After annotation is utilized to perform a method following each test method.
public class Junttest { @Test public void junitTest(){ System.out.println("Executing Junit test"); } @After public void afterTest(){ System.out.println("Executing after method"); } }
Output:
Executing JUnit test
Executing after method
#5. @AfterClass
The @AfterClass annotation is used to execute a method only once after implementing all test methods in a class. Like the @BeforeClass method, the @AfterClass method must be declared as static.
public class Junttest { @Test public void junitTest(){ System.out.println("Executing Junit test"); } @Test public void secondJunitTest(){ System.out.println("Executing second Junit test"); } @AfterClass Public static void afterClassTest(){ System.out.println("Executing after class method"); } }
Output:
Executing JUnit test
Executing second JUnit test
Executing after class method
JUnit assertions are exploited to validate particular conditions and cease the program’s execution if the conditions haven’t been met.
#6. Parameterized JUnit class:
A parameterized class is utilized to execute the identical scenario with several datasets.
Here is an example of passing multiple parameters in a JUnit test.
The @Parameters annotation is utilized to pass various data sets. In this example, we utilize a 2×2 dimensional array to depict the data:
@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); } }
Applying JUnit Assertions
JUnit assertEquals: This method verifies if two values are identical and fails the assertion if they differ.
This method can contrast Booleans, ints, Strings, floats, longs, chars, etc.
Syntax:
Assert.assertEqual(“anticipated value”, “actual value”);
Example:
Assert.assertEqual(“ABC”,”ABC”); // The strings are equal, so the assertion passes
Assert.assertEqual(“ABC”,”DEF”); // The strings vary, so the assertion fails
Assert.assertEqual(“Strings aren’t equal”, “ABC”,”DEF”); // A custom message will be displayed if the strings are different
Here’s an instance of applying JUnit assertions in a Selenium script:
String username=driver.findElement(By.id("username")).getText(); String password=driver.findElement(By.id("password")).getText(); Assert.assertEqual("Mismatch between both strings", username, password);
In the above instance, the assertion fails because the strings stored in the username and password variables do not correspond.
JUnit assertTrue: Returns true if a condition is true and fails the assertion if the condition is false.
Assert.assertTrue(“message”, condition);
Assert.assertTrue(“Both strings aren’t equal”, (“HelloWorld”).equals(“HelloWorld”));
In this example, the assertion passes because the condition is true, signifying that the two strings are identical. The specified message will be displayed if the assertion fails.
JUnit assertFalse: Returns true if a condition is false and fails the assertion if the condition is true.
Assert.assertFalse(“message”, condition);
Assert.assertFalse(“Both strings are identical”, (“Hello”).equals(“HelloWorld”));
In this example, the assertion doesn’t fail because the condition is false.
Conclusion:
JUnit is extensively employed by developers and testers due to its simplicity and efficacy. With JUnit, executing tests is straightforward and the results are clearly demonstrated via a green or red bar. JUnit provides its own library and annotations set, simplifying test development and execution. In this lesson, we explored commonly applied JUnit annotations in Selenium scripts and frameworks.
In our subsequent tutorial, we will delve further into framework design using JUnit, discussing more advanced subjects and techniques. This lesson will offer insights on designing frameworks with JUnit.
Tutorial #12 Upcoming: In the upcoming tutorial, we will examine TestNG, covering its functionalities and uses. TestNG is a more advanced testing framework proffering advantages for both developers and testers.