• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

SoftwareTestingo - Jira Selenium Protractor Testing SDLC Agile Methodology

Java Selenium Tutorial & Testing Interview Questions

  • Home
  • Interview Questions
  • Java
  • Java Programs
  • Test Cases
  • Selenium
  • Manual Testing
  • Difference
  • Search
SoftwareTestingo » Selenium » Selenium Tutorial » TestNG Method Overloading – Sample Example Program

TestNG Method Overloading – Sample Example Program

Last Updated on: November 30, 2019 By Softwaretestingo Editorial Board

What We Are Learn On This Post

  • TestNG Method Overloading In Details

TestNG Method Overloading: In Java Opps concept We have discussed the method overloading. Let m,e told you one more time method overloading is nothing but two or more methods have the same name, but they can have different parameters. If you want to know about the method overloading, then you can check the complete post by reading pour previous detail post on that.

TestNG Method Overloading In Details

After reading the complete post, one question may arise on your mind that, as method overloading is an important concept so can we use this feature in TestNG class also?

Let’s create a TestNG class with overloading methods:

package OverloadOverride;
import org.testng.annotations.Test;
public class OverloadedMethodsExample 
{
   @Test
   public void NormalMethod()
   {
      System.out.println(“Normal Method”);
   }

   // Overloaded Method
   @Test
   public void NormalMethod(String name)
   {
      System.out.println(“Overloaded Method”);
   }
}

Save and Run the program and see that output. I hope you will get an error by stating TestNG does not allow you to put any parameters to a TestNG annotated method.

But we can resolve this issue by using DataProvider or parameter to pass some input data to a method. You can check the below program:

Using Data Provider:

package OverloadOverride;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
public class OverloadedMethods 
{
   // Data provider which provides one attribute
   @Test(dataProvider="DemoData1")
   public void NormalMethod(String s)
   {
      System.out.println("Normal Method");
   }

   // Data provider which provides two attribute
   @Test(dataProvider="DemoData")
   public void NormalMethod(String s,int a)
   {
      System.out.println("Overloaded Method");
   }

   @DataProvider(name="DemoData")
   public static Object[][] dataProviderMethod()
   {
      return new Object[][] {{"Amod",123}};
   }

   @DataProvider(name="DemoData1")
   public static Object[][] dataProviderMethod1()
   {
      return new Object[][] {{"Amod"}};
   }
}

Using Parameters:

package OverloadOverride;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;
public class OverloadedMethods2 
{
   @Parameters({"name"})
   @Test
   public void NormalMethod(String name)
   {
      System.out.println("Normal Method");
   }

   @Parameters({"name1","age"})
   @Test
   public void NormalMethod(String name,int age)
   {
      System.out.println("Overloaded Method");
   }

}

Source: link

    Filed Under: Selenium Tutorial

    Reader Interactions

    Leave a Reply Cancel reply

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

    Primary Sidebar

    Join SoftwareTestingo Telegram Group

    Tutorials Important Links

    • Software Testing Jobs
    • Manual Testing Tutorial
    • Selenium Tutorial
    • Core Java Tutorial
    • TestNG Tutorial
    • Java Programs
    • Selenium Programs
    • Manual Test Cases
    • Interview Tips
    • Contact US
    • www.softwaretestingo.com

    Important Links

    • Software Testing Interview Questions
    • Agile Interview Questions
    • Manual Testing Interview Questions
    • Testing Interview Questions
    • Selenium Interview Questions
    • Selenium Real Time Interview Questions
    • Selenium WebDriver Interview Questions
    • Automation Framework Interview Questions
    • Appium Interview Questions
    • TestNG Interview Questions
    • Java Interview Questions
    • Core Java Interview Questions

    Categories

    Copyright © 2021 SoftwareTestingo.com ~ Contact Us ~ Sitemap ~ Privacy Policy