TestNG Method Overriding – With Sample Example

TestNG Method Overriding: Earlier, we have seen how we can achieve method overloading in a TestNG class, and similarly, in this post, we will see how we can make method overriding in a testNG class.

As we all know, method overriding is nothing but creating a method in a child class, which is already a similar method in the parent class, which is called method overriding. If you want to know the complete post about this, you can use this method overriding the link.

TestNG Method Overriding Example

So, let us discuss how we can achieve method overriding in Java:

First, create a superclass like the one below:

package InheritanceInTestNG;
import org.testng.annotations.Test;
public class SuperTestNgClass 
{

   @Test
   public void superTestNgMethod()
   {
      System.out.println(“Super testng method”);
   }

   @Test
   public void superTestNgAnotherMethod()
   {
      System.out.println(“Super testng Another method”);
   }
}

Create another child class like the one below:

package InheritanceInTestNG;
import org.testng.annotations.Test;
public class SubTestNGClass extends SuperTestNgClass
{

   // Overriding super class testng methods
   @Test
   public void superTestNgMethod()
   {
      System.out.println("Super testng method overrriden in sub class");
   }

   // Method of sub class
   @Test
   public void subTestNgMethod()
   {
      System.out.println("Sub testng method");
   }
}

Now, create a testng.xml file for the child class and run that.

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd">
<suite name="Suite">
  <test thread-count="5" name="Test">
    <classes>
      <class name="com.softwaretestingo.methodoverriding.TestNgMethodOverridingChildClass"/>
    </classes>
  </test> <!-- Test -->
</suite> <!-- Suite -->

If you look at the output, you will find out that it will execute all the child class methods with the overridden method.

Sub testng method
Super testng method overrriden in sub class
Super testng Another method

===============================================
Suite
Total tests run: 3, Passes: 3, Failures: 0, Skips: 0
===============================================

In our earlier discussion, we have seen that TestNG prioritizes and executes the test cases if we run both super and subclasses separately. But if we override, then all methods of the subclass with the overridden method will be executed, but the parent method of the superclass will be ignored.

Avatar for Softwaretestingo Editorial Board

I love open-source technologies and am very passionate about software development. I like to share my knowledge with others, especially on technology that's why I have given all the examples as simple as possible to understand for beginners. All the code posted on my blog is developed, compiled, and tested in my development environment. If you find any mistakes or bugs, Please drop an email to softwaretestingo.com@gmail.com, or You can join me on Linkedin.

Leave a Comment