Categories: Selenium TopicsTestNG Tutorials

TestNG Tutorials 44: Constructor with @Parameter Annotation

Hello Folks,

We have seen previously that how can we parameterize methods in a TestNG class.

The approach of using @Parameters annotations with each methods is not so good. Some demerits of this approach are as below:

  1. You need to include @Parameters annotation with each parameterized method.
  2. You need to accept as many as attributes in method signature passed in @Parameters annotation.
  3. If same parameter is used by multiple methods, you need to explicitly write that parameter name with @Parameters annotation for all methods consuming same parameter and in method signature.
  4. If you need to remove or add new parameters from methods, you need to edit both @Parameters annotation for desired methods and their method signature.

 

Can’t we do something so that all parameters will be initialized at once with values provided at run time which will be used by methods instead of individual Parameter annotation for each method ? 

Yes, we can. We can achieve that using Constructor concept of Java. 

We needs to create a constructor in TestNG class and use @Parameter annotation on that. Initialize all parameters in constructor. See an example below:

package Parameters;

import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;



public class ParametersExampleAtClonstructor {
        
        // List of parameters
        String p1;
        String p2;
        String p3;
        String p4;
        
        // Accept all parameters in Constructor from testng.xml
        @Parameters({"confBeforeParameter","testParameters1","testParameters2","confAfterParameter"})
        public  ParametersExampleAtClonstructor(String s1, String s2, String s3, String s4)
        {
                // Initialize all parameters 
                System.out.println("in constructor");
                p1=s1;
                p2=s2;
                p3=s3;
                p4=s4;
        }

        // Use whatever parameters are needed in methods
        // Note here I am not using any parameter in method signature and Parameters annotation
        @BeforeMethod
        public void configurationBeforeMethod()
        {
                System.out.println("Paramters for before configuration method: "+p1);
        }
        
        
        @Test
        public void testMethod()
        {
                System.out.println("Paramters one for test method: "+p2);
                System.out.println("Paramters two for test method: "+p3);
        }
        
        
        @AfterMethod
        public void configurationAfterMethod()
        {
                System.out.println("Paramters for after configuration method: "+p4);
        }
}

TestNG xml:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite">
        <test thread-count="5" name="Test">
                <parameter name="confBeforeParameter" value="confBeforeParameter" />
                <parameter name="testParameters1" value="testParameters1" />
                <parameter name="testParameters2" value="testParameters2" />
                <parameter name="confAfterParameter" value="confAfterParameter" />
                <classes>
                        <class name="Parameters.ParametersExampleAtClonstructor" />
                </classes>
        </test> <!-- Test -->
</suite> <!-- Suite -->

Output:

If you want to add or remove parameters, you can do easily by modification in constructor instead of multiple places. Hope this concept will be very much useful when you need to deal with multiple parameters.

More about TestNG in upcoming posts. Stay tuned.

If you have any doubt, feel free to comment below.
If you like my posts, please like, comment, share and subscribe.
#ThanksForReading
#HappySelenium

 

Author: Amod Mahajan

My name is Amod Mahajan and I am an IT employee with 4+ years of experience in Software testing and staying in Bengaluru. My area of interest is Automation testing. I started from basics and went throw so many selenium tutorials. Thanks to Mukesh Otwani as his tutorials are easy and cover basics to advance. I have habit of exploring concepts by deep diving. I used to make notes. I thought of sharing my knowledge through posts and now I am here. #KeepLearning #ShareLearning

Amod Mahajan

My name is Amod Mahajan and I am an IT employee with 4+ years of experience in Software testing and staying in Bengaluru. My area of interest is Automation testing. I started from basics and went throw so many selenium tutorials. Thanks to Mukesh Otwani as his tutorials are easy and cover basics to advance. I have habit of exploring concepts by deep diving. I used to make notes. I thought of sharing my knowledge through posts and now I am here. #KeepLearning #ShareLearning

Recent Posts

API Testing Tutorial Part 14 – Sending First GET Request in Postman

Hello Folks, As part of our API Testing series, we will see "Sending first GET request in Postman". First of all…

4 days ago

TestNG Tutorials 53: DataProvider in TestNG – Is It Mandatory To Have Return Type as Object in DataProvider Method

Hello Guys, DataProvider method in TestNG is a way to provide test data to Test annotated methods in a TestNG…

5 days ago

API Testing Tutorial Part 13 – Introduction of Postman Tool

Hello Folks, As part of our API Testing series, we will see "Introduction of Postman Tool" in this post. Postman is…

2 weeks ago

API Testing Tutorial Part 12 – Tools For Manual & Automation API Testing

Hello Folks, As part of our API Testing series, we will see "Tools to test SOAP and REST APIs manually and…

2 weeks ago

API Testing Tutorial Part 11 – Difference Between SOAP and Rest Web Services or API

Hello Folks, As part of our API Testing series, we will see "Difference between SOAP and REST web services" in…

2 weeks ago

API Testing Tutorial Part 10 – Introduction of SOAP (Simple Object Access Protocol)

Hello Folks, As part of our API Testing series, we will see “Introduction of SOAP” in this post. SOAP stands…

2 weeks ago