TestNG Method Overloading: We discussed the method overloading in the Java Opps concept. Let me tell you one more time: method overloading is nothing but two or more methods that have the same name, but they can have different parameters.
If you want to know about the method overloading, you can check the complete post by reading the previous detailed post.
TestNG Method Overloading In Details
After reading the complete post, one question may arise: as method overloading is an important concept, can we also use this feature in the TestNG class?
Let’s create a TestNG class with overloading methods:
package com.softwaretestingo.testng.overloading; import org.testng.annotations.Test; public class MethodOverloadingParentClass { @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 stating that TestNG does not allow you to put any parameters to a TestNG annotated method.
Normal Method 08:32:20.334 [main] ERROR org.testng.internal.Utils - [Error] org.testng.TestNGException: Cannot inject @Test annotated Method [NormalMethod] with [class java.lang.String]. PASSED: com.softwaretestingo.testng.overloading.MethodOverloadingParentClass.NormalMethod FAILED: com.softwaretestingo.testng.overloading.MethodOverloadingParentClass.NormalMethod =============================================== Default test Tests run: 2, Failures: 1, Skips: 0 =============================================== =============================================== Default suite Total tests run: 2, Passes: 1, Failures: 1, Skips: 0 ===============================================
But we can resolve this issue using DataProvider or a Parameter to pass some input data to a method. You can check the below program:
TestNG Method Overloading With DataProvider
In the below program, we have used the DataProvider concept to demonstrate how TestNG Method Overloading can be achieved with the help of the DataProvider concept of TestNG:
package com.softwaretestingo.testng.overloading; import org.testng.annotations.DataProvider; import org.testng.annotations.Test; public class MethodOverloadingWithDataProvider { // 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[][] {{"SoftwareTestingo",2023}}; } @DataProvider(name="DemoData1") public static Object[][] dataProviderMethod1() { return new Object[][] {{"SoftwareTestingo"}}; } }
Output:
Normal Method Overloaded Method PASSED: com.softwaretestingo.testng.overloading.OverloadedMethods.NormalMethod("SoftwareTestingo", 2023) PASSED: com.softwaretestingo.testng.overloading.OverloadedMethods.NormalMethod("SoftwareTestingo") =============================================== Default test Tests run: 2, Failures: 0, Skips: 0 =============================================== =============================================== Default suite Total tests run: 2, Passes: 2, Failures: 0, Skips: 0 ===============================================
TestNG Method Overloading With Parameter
Now, we will try to achieve method overloading in TestNG with the help of parameters.
package com.softwaretestingo.testng.overloading; import org.testng.annotations.Parameters; import org.testng.annotations.Test; public class MethodOverloadingWithParameter { @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"); } }
testng.xml file
<?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"> <parameter name="name" value="Testing"></parameter> <parameter name="name1" value="SoftwareTestingO"></parameter> <parameter name="age" value="2023"></parameter> <classes> <class name="com.softwaretestingo.testng.overloading.MethodOverloadingWithParameter" /> </classes> </test> <!-- Test --> </suite> <!-- Suite -->
When we execute the above XML file, the output will be:
Normal Method Overloaded Method =============================================== Suite Total tests run: 2, Passes: 2, Failures: 0, Skips: 0 ===============================================