Make Selenium Easy

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
	public  ParametersExampleAtClonstructor(String s1, String s2, String s3, String s4)
		// Initialize all parameters 
		System.out.println("in constructor");

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

TestNG xml:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "">
<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" />
			<class name="Parameters.ParametersExampleAtClonstructor" />
	</test> <!-- Test -->
</suite> <!-- Suite -->


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.


Table of Contents

Author: Amod Mahajan

A software Tester who is paid to judge products developed by others. Currently getting paid in American Dollars. Writing technical posts and creating YouTube videos are my hobbies.

Leave a Reply

Please wait...

Subscribe to new posts to become automation expert

Want to be notified when my new post is published? Get my posts in your inbox.

TestNG Tutorials 44: Constructor with @Parameter Annotation

by Amod Mahajan time to read: 2 min
%d bloggers like this: