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
Leave a Reply