Quick Contact

    About JUnit and TestNG

    JUnit

    JUnit is a unit testing framework for Java programming language. A Unit Test Case is a part of code, which ensures that another part of code (method) works as expected. A formal written unit test case is characterized by a known input and an expected output, which is worked out before the test is executed. The known input should test a precondition and the expected output should test a post- condition.

    JUnit promotes the idea of “first testing then coding”, which emphasizes on setting up the test data for a piece of code that can be tested first and then implemented. This approach is like “test a little, code a little, test a little, code a little.” It increases the productivity of the programmer and the stability of program code, which in turn reduces the stress on the programmer and the time spent on debugging.

    Features of JUnit

    The Features of JUnit are:

    • JUnit is an open source framework, which is used for writing and running tests.
    • It provides annotations to identify test methods.
    • It provides assertions for testing expected results.
    • It provides test runners for running tests.
    • JUnit tests allow you to write codes faster, which increases quality.
    • JUnit tests can be run automatically and they check their own results and provide immediate feedback. There’s no need to manually comb through a report of test results.
    • JUnit tests can be organized into test suites containing test cases and even other test suites.
    • JUnit shows test progress in a bar that is green if the test is running smoothly, and it turns red when a test fails.
    Adding JUnit library in Java project
    1. Right click on Java project->Build Path->Configure Build path
    2. Click Libraries->Add Library
    3. Click on Junit.
    4. Select Junit4->Finish
    5. Click OK.
    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.

    • @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);
      }
      
    • @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
    • @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
      
    • @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");
                 }
                }
            
      Running JUnit test 
            Running after method
    • @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
    TestNG

    TestNG is a testing framework inspired from JUnit and NUnit, but introducing some new functionalities that make it more powerful and easier to use. TestNG is an open source automated testing framework; where NG means NextGeneration. TestNG gives the developer the ability to write more flexible and powerful tests.

    Features of TestNG

    The features of TestNG are:

    • Supports annotations.
    • TestNG uses more Java and object oriented features.
    • Supports testing integrated classes
    • Separates compile-time test code from run-time configuration/data info.
    • Flexible runtime configuration.
    • Supports Dependent test methods, parallel testing, load testing, and partial failure.
    • Flexible plug-in API.
    • Support for multi threaded testing.
    TestNG Installation in Eclipse

    The steps to Download and install TestNG on eclipse are:

    1. Launch eclipse IDE -> Click on the Help option within the menu -> Select “Eclipse Marketplace..” option within the dropdown.
    2. Enter the keyword “TestNG” in the search textbox and click on “Go” button as shown below.
    3. Click on the “Go” button, the results matching to the search string would be displayed. Now click on the Install button to install TestNG.
    4. Click on the Install button, you will get prompt with a window to confirm the installation. Click on “Confirm” button.
    5. In the next step, the application would prompt you to accept the license and then click on the “Finish” button.
    6. The installation is initiated now and the progress can be seen as follows:
    7. Restart eclipse so as to reflect the changes made.
    8. Upon restart, verify the TestNG installation by navigating to “Preferences” from “Window” option in the menu bar. Refer the following figure for the same.
    Creation of Sample TestNG Project
    1. Click on the File option within the menu -> Click on New -> Select Java Project.
    2. Enter the project name as “DemoTestNG” and click on “Next” button. As a concluding step, click
      on the “Finish” button and your Java project is ready.
    3. The next step is to configure the TestNG library into the newly created Java project. For the same, click on the “Libraries” tab under Configure Build Path. Click on “Add library” as shown below.
    4. Select TestNG and click on the “Next” button as shown below in the image. In the end, click on the “Finish” button.
    5. The TestNG is now added to the Java project and the required libraries can be seen in the package explorer upon expanding the project.
    Creating TestNG class
    1. Expand the “DemoTestNG” project and traverse to “src” folder. Right-click on the “src” package and navigate to New -> Other.
    2. Expand TestNG option and select “TestNG” class option and click on the “Next” button.
    3. Furnish the required details as following. Specify the Source folder, package name and the
      TestNG class name and click on the Finish button.
    4. The above mentioned TestNG class would be created with the default schema.
    Test Scenario
    • Launch the browser and open “gmail.com”.
    • Verify the title of the page and print the verification result.
    • Enter the username and Password.
    • Click on the Sign in button.
    • Close the web browser.
    Code:
    package TestNG;
    import org.openqa.selenium.By;
    import org.openqa.selenium.WebDriver; 
    import org.openqa.selenium.WebElement;
    import org.openqa.selenium.firefox.FirefoxDriver; 
    import org.testng.Assert;
    import org.testng.annotations.Test;
    
    public class DemoTestNG {
    
    public WebDriver driver = new FirefoxDriver();
    
    String appUrl = "https://accounts.google.com";
    
    @Test
    
    public void gmailLogin() {
    
    // launch the firefox browser and open the application url driver.get("https://gmail.com");
     
    
    // maximize the browser window
    
    driver.manage().window().maximize();
    
    // declare and initialize the variable to store the expected title
    //of the webpage.
    
    String expectedTitle = " Sign in - Google Accounts "
    
    // fetch the title of the web page and save it into a string
    //variable
    
    String actualTitle = driver.getTitle();
    
    Assert.assertEquals(expectedTitle,actualTitle);
    
    // enter a valid username in the email textbox
    
    WebElement username=
    driver.findElement(By.id("Email")
    );
    
    username.clear();
    
    username.sendKeys("TestSelenium");
    
    // enter a valid password in the password textbox
    
    WebElement password = driver.findElement(By.id("Passwd"));
    
    password.clear();
    
    password.sendKeys("password123");
    
    // click on the Sign in button
    
    WebElement SignInButton = 
    driver.findElement(By.id("signIn"));
    
    SignInButton.click();
    
    // close the web browser
    
    driver.close();
    
    }
    
    }
    
    
    Code Explanation with respect to TestNG

    @Test – @Test is one of the TestNG annotations. This annotation lets the program execution to know that method annotated as @Test is a test method. To be able to use different TestNG annotations, we need to import the package “import org.testng.annotations.*”.

    There is no need of main() method while creating test scripts using TestNG. The program execution is done on the basis of annotations.

    In a statement, we used Assert class while comparing expected and the actual value. Assert class is used to perform various verifications. To be able to use different assertions, we are required to
    import “import org.testng.Assert”.

    Executing the TestNG script
    1. Right click anywhere inside the class within the editor or the java class within the package explorer, select “Run As” option and click on the “TestNG Test”.
    TestNG result is displayed into two windows:
    • Console Window
    • TestNG Result Window

    Refer the below screencasts for the result windows:

    HTML Reports

    TestNG comes with a great capability of generating user readable and comprehensible HTML reports for the test executions. These reports can be viewed in any of the browsers and it can also be viewed using Eclipse’s build –in browser support.

    To generate the HTML report, follow the below steps:
    1. Execute the newly created TestNG class. Refresh the project containing the TestNG class by right-clicking on it and selecting “Refresh” option.
    2. A folder named as “test-output” shall be generated in the project at the “src” folder level. Expand the “test-output” folder and open on the “emailable-report.html” file with the Eclipse browser. The HTML file displays the result of the recent execution.
    3. The HTML report shall be opened within the eclipse environment. Refer the below image for the same.
    What is JAVA Framework

    Frameworks are large bodies (usually classes) of prewritten code to which new custom code is added to solve a problem in a specific domain. Thus the framework uses custom code because it is usually the framework that is in control. Technically, a framework is a collection of software libraries or components which provide a defined application programming interface (API). The framework is used by calling its methods, inheritance, and supplying “callbacks”, listeners, or other implementations of the Observer pattern.

    A very common example of JAVA framework are GUI frameworks, eg Java’s Swing and AWT classes. These framework have a huge amount of code to manage the user interface, and there is inversion of control because one starting the GUI framework, then wait for it to call listeners.

    The JAVA collections classes are sometimes called a framework due of the size and complexity.

    Test Annotations

    The Annotations are lines of code that are inserted in the program/ business logic to control how the methods below them are to be run. They are always preceded by the @ symbol. The annotations differ depending on your project requirements. However, the flow of execution will be the same. Both JUnit and TestNG uses annotations. For example TestNG uses @BeforeMethod ,@AfterMethod similar to @Before ,@After in JUnit4.

    The list of annotations are:

    Description TestNG JUnit 4
    Test annotation @Test @Test
    Executes before the first test method is invoked in the current class @BeforeClass @BeforeClass
    Executes after all the test methods in the current class @AfterClass @AfterClass
    Executes before each test method @BeforeMethod @Before
    Executes after each test method @AfterMethod @After
    annotation to ignore a test @Test(enable=false) @ignore
    annotation for exception @Test(expectedExceptions =
    ArithmeticException.class)
    @Test(expected = ArithmeticException.class)
    timeout @Test(timeout = 1000) @Test(timeout = 1000)
    Executes before all tests in the suite @BeforeSuite n/a
    Executes after all tests in the suite @AfterSuite n/a
    Executes before a test runs @BeforeTest n/a
    Executes after a test runs @AfterTest n/a
    Executes before the first test method is invoked that belongs to any of these groups is invoked @BeforeGroups n/a
    run after the last test method that belongs to any of the groups here @AfterGroups n/a

    Example:

    1. Create a java class file name TestngAnnotation.java in C:\>TestNG_WORKSPACE to test annotations.

    2. import org.testng.annotations.Test;
      import org.testng.annotations.BeforeMethod; 
      import org.testng.annotations.AfterMethod; 
      import org.testng.annotations.BeforeClass; 
      import org.testng.annotations.AfterClass; 
      import org.testng.annotations.BeforeTest; 
      import org.testng.annotations.AfterTest; 
      import org.testng.annotations.BeforeSuite; 
      import org.testng.annotations.AfterSuite;
      
      public class TestngAnnotation {
      // test case 1 
      @Test
      public void testCase1() { 
      System.out.println("in test case 1");
      }
      
      // test case 2 
      @Test
      public void testCase2() { 
      System.out.println("in test case 2");
      }
      
      
      @BeforeMethod
      public void beforeMethod() { 
      System.out.println("in beforeMethod");
      }
       
      @AfterMethod
      public void afterMethod() { 
      System.out.println("in afterMethod");
      }
      
      public void afterTest() { 
      System.out.println("in afterTest");
      }
      
      @BeforeSuite
      public void beforeSuite() { 
      System.out.println("in beforeSuite");
      }
      
      @AfterSuite
      public void afterSuite() { 
      System.out.println("in afterSuite");
      }
      
      
      }
      	
    3. Create the file testng.xml in C:\>TestNG_WORKSPACE to execute annotations.
    4. <?xml version = "1.0" encoding = "UTF-8"?>
      <!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
      
      <suite name = "Suite1">
      <test name = "test1">
      <classes>
      <class name = "TestngAnnotation"/>
      </classes>
      </test>
      </suite>
      
    5. Compile the Test case class using javac.
    6. C:\TestNG_WORKSPACE>javac TestngAnnotation.java
    7. Run the testng.xml, which will run the test case defined in the provided Test Case class.
    8. C:\TestNG_WORKSPACE>java org.testng.TestNG testng.xml

    Output:

    in beforeSuite 
    	in beforeTest 
    	in beforeClass 
    	in beforeMethod 
    	in test case 1 
    	in afterMethod 
    	in beforeMethod 
        in test case 2 
        in afterMethod 
        in afterClass 
        in afterTest
        in afterSuite
    ===============================================
    Suite
    Total tests run: 2, Failures: 0, Skips: 0
    
    • First of all, beforeSuite() method is executed only once.
    • Lastly, the afterSuite() method executes only once.
    • Even the methods beforeTest(), beforeClass(), afterClass(), and afterTest() methods are executed only once.
    • beforeMethod() method executes for each test case but before executing the test case.
    • afterMethod() method executes for each test case but after executing the test case.
    • In between beforeMethod() and afterMethod(), each test case executes.
    Executing Tests in Sequence
    JUnit

    In JUnit @FixMethodOrder(MethodSorters.NAME_ASCENDING) is used to run the test methods by method name, in lexicographic order.

    Example:

    package com.mkyong;
    import org.junit.FixMethodOrder; 
    import org.junit.Test;
    import org.junit.runners.MethodSorters; 
    import static org.hamcrest.CoreMatchers.is; 
    import static org.junit.Assert.assertThat;
    //Sorts by method name 
    @FixMethodOrder(MethodSorters.NAME_ASCENDING) 
    public class ExecutionOrderTest {
    @Test
    public void testB() { 
    assertThat(1 + 1, is(2));
    }
    @Test
    public void test1() { 
    assertThat(1 + 1, is(2));
    }
    @Test
    public void testA() { 
    assertThat(1 + 1, is(2));
    }
    @Test
    
    public void test2() {
    
    
    assertThat(1 + 1, is(2));
    }
    @Test
    public void testC() { 
    assertThat(1 + 1, is(2));
    }
    }
    

    Output

    test1 
    test2 
    testA 
    testB 
    testC
    TestNG

    TestNG provides three ways to set ordering for tests:

    • Using preserve-order in the testng.xml file

      The preserve-order attribute in the testng.xml file is used to have TestNG run the tests in the order they appear in the XML file:

      Example:

      <test name="OrderedTestNGTests" preserve-order="true">
      <classes>
      <class name="TestNGTestClass">
      <methods>
      <include name="testOne" />
      <include name="testTwo" />
      </methods>
      </class>
      </classes>
      </test>
      
    • Using the priority attribute

      The priority attribute in @Test annotation can be used to prioritize test methods and determine the order in which they are run

      Example:

      public class TestNGPrioritized { 
      			@Test(priority = 3)
                  public void testThree() {
              }
                  @Test(priority = 1) 
                  public void testOne() {
              }
                  @Test(priority = 2) 
                  public void testTwo() {
              }
              }
            
    • Using dependencies

      In TestNG, we can have tests and test suites depend on other tests or test suites. This also implicitly defines the order in which the tests are executed. For example, when test A depends on test B, test B will automatically be run before test A. These dependencies can be defined in code.

      Example:

      public class TestNGOrderedTests { 
      			@Test(dependsOnMethods = {"parentTest"}) 
      			public void childTest() {
                }
                  @Test
                  public void parentTest() {
                }
                }
              

    Alternatively, we can define dependencies on group level in the testng.xml file:

    <test name="TestNGOrderedTests">
    <groups>
    <dependencies>
    <group name="parenttests" />
     
    
    <group name="childtests" depends- on="parenttests" />
    </dependencies>
    </groups>
    </test>
    
    Assertions

    Selenium commands are of three types:

    • Actions: generally manipulate the state of the application like “click this link” and “select that option”. If an Action fails, or has an error, the execution of the current test is stops.
    • Accessors: examine the state of the application and store the results in variables, e.g. “Title”.
    • Assertions verify that the state of the application is same to what we are expecting.

    Assertions provide a means to validate any kind of test. The Assertion results are based on the comparison of actual & expected results. A test is considered passed only when the Assertions show no Exceptions. Selenium Assertions can be of three types: “assert”, “verify”, and” waitFor”.

    When an “assert” fails, the test is aborted.

    When a “verify” fails, the test will continue execution, logging the failure.

    A “waitFor” command waits for some condition to become true. They will fail and halt the test if the condition does not become true within the current timeout setting.

    The various used assertions are:

    • Assert Equals: Assert Equals works by comparing the expected condition with that of the actual condition based on which the test results are displayed.
    • Assert Not Equals: “Assert Not Equals” serves the purpose of negation testing for Testers. If the actual and expected do not match, then the test Script passes else it fails.
    • Assert True: This Assertion passes the Test Step only when the boolean value returned is “True”
    • Assert False: “Assert False” passes the Test Step only when the boolean value returned is “False”
    • Assert Null: This Assertion verifies if the object under test is null, and the passes the same if the result is so.
    • Assert Not Null: This Assertion functions opposite to that of “Assert Null”. Thus this Assertion verifies if the object under test is not null, and the passes the same if the result is so.
    • Assert Same: This Assertion checks that two objects refer to the same object, if it does, then the Assertion passes else it fails the same.
    • Assert Not Same: This Assertion checks that two objects do not refer to the same object, if it does than the Assertion passes else it fails the same.
    Error Collectors

    ErrorCollector in JUnit is a rule that allows execution of a test to continue after the first problem is found. Error collector objective is to collect all the error which comes up with script execution and report it only at the end. To give more understanding consider q case while testing script any line of code fails due to network failure, assertion failure, or any other reason. In that situation, executing test script can continue by using “error collector.”

    JUnit uses @Rule annotation which is used to create an object of error collector. Once the object for error collector is created the errors can be added into the object using method addError. Throwable is the super class of Exception and Error class in Java. The benefit of adding all errors in an Error Collector is that one can verify all the errors at once. Also, if the script fails in the middle, it can still continue executing it.

    Example:

    • Create a class and a rule to collect all the errors and add all the errors using addError(throwable).
    • import org.junit.Assert; 
              	import org.junit.Rule; 
              	import org.junit.Test;
                  import org.junit.rules.ErrorCollector; 
                  public class ErrorCollectorExample {
               @Rule
                   public ErrorCollector 
                   collector = new ErrorCollector();
      
                @Test
                   public void example() {
                   collector.addError(new Throwable("There is an error in first line"));
                   collector.addError(new Throwable("There is an error in second line"));
      
                   System.out.println("Hello"); 
                   try {
                   Assert.assertTrue("A " == "B");
      
                   catch (Throwable t) {
      }             
      collector.addError(t);
      }
      System.out.println("World!!!!");
      
      }
      }
      
    • Add above test class in a test runner and execute it to collect all errors.
    • import org.junit.runner.JUnitCore; 
      import org.junit.runner.Result;
      import org.junit.runner.notification.Failure;
      
      public class TestRunner {
      public static void main(String[] args) {
      
      Result result = 
      JUnitCore.runClasses(ErrorCollectorExample.class);
      
      for (Failure failure : result.getFailures()) { 
      
      System.out.println(failure.toString());
      }
      System.out.println("Result=="+result.wasSuccessful());
      
      }
      }
      
    How to Parameterize Test Cases

    Parameterized tests allow a developer to run the same test over and over again using different values. There are five steps to create a parameterized test.

    • Annotate test class with @RunWith(Parameterized.class).
    • Create a public static method annotated with @Parameters that returns a Collection of Objects (as Array) as test data set.
    • Create a public constructor that takes in what is equivalent to one “row” of test data.
    • Create an instance variable for each “column” of test data.
    • Create your test case(s) using the instance variables as the source of the test data.

    Example:

    1. Create a java class to be tested, say, PrimeNumberChecker.java in C:\>JUNIT_WORKSPACE.
    2. 		public Boolean validate(final Integer primeNumber) { 
      		for (int i = 2; i < (primeNumber / 2); i++) {
              if (primeNumber % i == 0) { 
              return false;
      }
      }
      return true;
      }
      }
      
    3. Create a java test class, say, PrimeNumberCheckerTest.java. Create a java class file named PrimeNumberCheckerTest.java in C:\>JUNIT_WORKSPACE.
    4. import java.util.Arrays; 
      import java.util.Collection;
      import org.junit.Test; 
      import org.junit.Before;
      import org.junit.runners.Parameterized;
      import org.junit.runners.Parameterized.Parameters; 
      import org.junit.runner.RunWith;
      import static org.junit.Assert.assertEquals; 
      @RunWith(Parameterized.class)
      public class PrimeNumberCheckerTest { 
      private Integer inputNumber; 
      private Boolean expectedResult;
      private PrimeNumberChecker primeNumberChecker; 
      @Before
      public void initialize() {
      primeNumberChecker = new PrimeNumberChecker();
      
      }
      // Each parameter should be placed as an argument here
      // Every time runner triggers, it will pass the arguments
      // from parameters we defined in primeNumbers() method 
      public PrimeNumberCheckerTest(Integer inputNumber, Boolean
      expectedResult) {
      this.inputNumber = inputNumber; this.expectedResult = 
      expectedResult;
      }
      @Parameterized.Parameters
      public static Collection primeNumbers() { 
      return Arrays.asList(new Object[][] {
      { 2, true },
      { 6, false },
      { 19, true },
      { 22, false },
      { 23, true }
       
      });
      }
      // This test will run 4 times since we have 5 parameters defined
      @Test
      public void testPrimeNumberChecker() { 
      System.out.println("Parameterized Number is : " +
      inputNumber);
      assertEquals(expectedResult, primeNumberChecker.validate(inputNumber));
      }
      }
      
    5. Create a java class file named TestRunner.java in C:\>JUNIT_WORKSPACE to execute test case(s).
    6. import org.junit.runner.JUnitCore; 
      import org.junit.runner.Result;
      import org.junit.runner.notification.Failure; 
      public class TestRunner {
      public static void main(String[] args) {
      Result result = JUnitCore.runClasses(PrimeNumberCheckerTest.class);
      for (Failure failure : result.getFailures()) { 
      System.out.println(failure.toString());
      }
      
      System.out.println(result.wasSuccessful());
      }
      }
      
    7. Compile the PrimeNumberChecker, PrimeNumberCheckerTest and Test Runner classes using javac.
    8. C:\JUNIT_WORKSPACE>javac 
      PrimeNumberChecker.java 
      PrimeNumberCheckerTest.java
      TestRunner.java
      Now run the Test Runner, which will run the test cases defined in the provided Test Case class.
       
      C:\JUNIT_WORKSPACE>java TestRunner
      

    Output:

    Parameterized Number is : 2 
    Parameterized Number is : 6 
    Parameterized Number is : 19 
    Parameterized Number is : 22 
    Parameterized Number is : 23 
    True
    Summary
    • JUnit is a unit testing framework for Java programming language
    • A Unit Test Case is a part of code, which ensures that another part of code (method) works as expected
    • JUnit promotes the idea of “first testing then coding”
    • Frameworks are large bodies (usually classes) of prewritten code to which new custom code is added to solve a problem in a specific domain
    • The Annotations are lines of code that are inserted in the program/ business logic to control how the methods below them are to be run
    • Assertions provide a means to validate any kind of test

    Copyright 1999- Ducat Creative, All rights reserved.