Skip to content

Make Selenium Easy

And Keep It That Way

  • Home
  • Share
  • Toggle search form

TestNG Tutorials 47: Marking a Parameter as Optional in TestNG

Posted on 03/16/2025 By admin

Hello folks,

When we parametrize methods in a TestNG class, we must need to pass parameter values from testng xml. If we do not pass we get an exception. We will see an example below:

TestNG class:

[java]package Parameters;

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

public class ParameterTest { @Test @Parameters({“testParameters1″,”testParameters2”}) public void testMethod(String testParameters1, String testParameters2) { System.out.println(“Paramters one for test method: “+testParameters1); System.out.println(“Paramters two for test method: “+testParameters2);

}

}
[/java]

Testng xml without parameters:

[xml]

[/xml]

Output:

When we define a parameter in a method of a testng class, TestNG expects either you declare parameter as optional or pass it from testng xml. If you do not do either, you will get exception as above stating the same. 

How to set a parameter as Optional?

TestNG provides you flexibility of declaring parameters as optional. When we declare a parameter as optional, TestNG first looks in testng xml if parameter value is provided. If provided, it will use that value otherwise it will assign default value to parameter. It will not throw any exception. 

We just need to append parameter name with “@Optional” (from org.testng.annotations.Optional) annotation. Example:

[java]package Parameters;

import org.testng.annotations.Optional; import org.testng.annotations.Parameters;

import org.testng.annotations.Test;

public class ParameterTest { @Test @Parameters({“testParameters1″,”testParameters2”}) // I used Optional annotation with parameter declaration public void testMethod(@Optional String testParameters1, @Optional String testParameters2) { System.out.println(“Paramters one for test method: “+testParameters1); System.out.println(“Paramters two for test method: “+testParameters2);

}

}
[/java]

Testng xml without parameters:

I am not passing any value to parameter from testng xml here. 

[xml]

[/xml]

Output:

Learn here that how TestNG assigned default values to parameters as null. Now you can even handle if value is passed and perform actions. It also helps you to skip test at run time if parameter value is not provided.

If we pass parameter value from testng xml, it will use that value. Example:

Testng xml:

[xml]

[/xml]

Output:

Let’s learn something new here:

Now there is a catch. We declared data type of parameter as String, what happens if we declare data type as int or float or any primitive type? See in an example below:

[java]
package Parameters;

import org.testng.annotations.Optional; import org.testng.annotations.Parameters;

import org.testng.annotations.Test;

public class ParameterTest { @Test @Parameters({“testParameters1″,”testParameters2”}) public void testMethod(@Optional String testParameters1, @Optional int testParameters2) { System.out.println(“Paramters one for test method: “+testParameters1); System.out.println(“Paramters two for test method: “+testParameters2);

}

}
[/java]

Testng xml without parameters:

[xml]

[/xml]

Output:

I have seen many people facing this issue. Solution is do not use primitive data type. Use wrapper of primitive. E.g. Integer for int.

Example:

[java]
package Parameters;

import org.testng.annotations.Optional; import org.testng.annotations.Parameters;

import org.testng.annotations.Test;

public class ParameterTest { @Test @Parameters({“testParameters1″,”testParameters2”}) public void testMethod(@Optional String testParameters1, @Optional Integer testParameters2) { System.out.println(“Paramters one for test method: “+testParameters1); System.out.println(“Paramters two for test method: “+testParameters2);

}

}
[/java]

Output:

Now you can see TestNG were able to pass default values as null to parameters.

How to override default value of parameters?

We see here that TestNG passes default value as null to parameters declared as Optional when value to it is not passed from testng xml. But what if you do not want default value as null. We can override default values as well.

You need to pass another parameter name with Optional annotation as an attribute whose value will be used if value of actual parameter is not passed. Example: @Optional(“optionalParames”) String testParameters1

If value of “testParameters1″ is not passed from testng xml, TestNG will see if value of “optionalParames” is passed. If it is passed, that value will be used. If none of parameter value is passed, default value will be assigned by TestNG. 

Example below:

[java]package Parameters;

import org.testng.annotations.Optional; import org.testng.annotations.Parameters;

import org.testng.annotations.Test;

public class ParameterTest { @Test @Parameters({“testParameters1″,”testParameters2”}) /* * If “testParameters1” not found in testng xml use value provided for parameter “optionalParames”. * If none is present in testng xml, use null. */ public void testMethod(@Optional(“optionalParames”) String testParameters1, @Optional(“optionalParames”) String testParameters2) { System.out.println(“Paramters one for test method: “+testParameters1); System.out.println(“Paramters two for test method: “+testParameters2);

}

}
[/java]

Testng xml:

[xml]

[/xml]

Output:

Hope you have learnt new things about Optional 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

Uncategorized

Post navigation

Previous Post: Amod Mahajan
Next Post: REST Assured Tutorial 29 – How to create POJO classes of a JSON Object Payload

Related Posts

API Testing Tutorial Part 5 – Safe Methods in HTTP Methods Uncategorized
Selenium Uncategorized
June 15, 2019 – Make Selenium Easy Uncategorized
image – Make Selenium Easy Uncategorized
May 24, 2017 – Make Selenium Easy Uncategorized
Git Tutorial 3 – Install GIT On Windows – Step By Step Uncategorized

Recent Posts

  • Getting Started with Selenium 4: What Is New and How to Upgrade from Selenium 3
  • Manual Testing
  • Baby Steps To Become Efficient Selenium-Java Automation Tester
  • Features of Selenium 4.0.0 Release – Java Binding
  • Part 1: Handling Drop-down Created Using SELECT Tag In Selenium

Recent Comments

No comments to show.

Archives

  • April 2026
  • April 2025
  • March 2025
  • February 2025
  • January 2025
  • December 2024
  • November 2024
  • October 2024
  • September 2024
  • August 2024
  • April 2024
  • March 2024
  • February 2024
  • December 2023
  • October 2023
  • August 2023
  • November 2022
  • September 2022
  • August 2022
  • July 2022
  • May 2022
  • March 2022
  • October 2021
  • April 2021
  • March 2021
  • January 2021
  • December 2020
  • October 2020
  • September 2020
  • August 2020
  • June 2020
  • May 2020
  • April 2020
  • March 2020
  • February 2020
  • January 2020
  • December 2019
  • November 2019
  • October 2019
  • September 2019
  • August 2019
  • May 2019
  • December 2018
  • November 2018
  • October 2018
  • September 2018
  • August 2018
  • July 2018
  • January 2018

Categories

  • Getting Started
  • Uncategorized

Copyright © 2026 Make Selenium Easy.

Powered by PressBook Masonry Dark