Optional parameters work similarly to the default case in the parameterization in TestNG. We use the optional parameter when no other parameter gets defined for that test case method. Additionally, the @Optional annotation declares the optional parameter. We don’t define the @Optional parameter above the test method definition but alongside where the method is declared. Subsequently, the following code snippet demonstrates the declaration of the optional parameters in TestNG:
import org.testng.annotations.Optional;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;
public class Params
{
@Test
@Parameters ("message")
public void OP( @Optional("Optional Parameter Selected") String message) {
System.out.println(message);
}
}
import org.testng.annotations.Optional;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;
public class Params
{
@Test
@Parameters ("message")
public void OP( @Optional("Optional Parameter Selected") String message) {
System.out.println(message);
}
}