3

When number of arguments to a method exceeds 7, checkstyle error (i.e., More than 7 parameters (found 8). [ParameterNumber]) would be thrown. Hence, for below method also it is throwing. Generally, checkstyle error can be avoided by using String array or Hashmap.

But, how to avoid here being the method arguments having @Optional annotation?

@Parameters({ "test1", "test2", "test3", "test4", "test5", "test6", "test7", "test8" })
@BeforeTest
public void beforeTest(@Optional("value1") String test1, @Optional("value2") String test2, @Optional("value3") String test3, @Optional("value4") String test4, @Optional("value5") String test5, @Optional("value6") String test6, @Optional("value7") String test7, @Optional("value8") String test8) {
    ....
}

One way is to increase the parameter limit in checkstyle.xml.

But, looking for if there is any better solution.

Linus Fernandes
  • 498
  • 5
  • 30
Sandeep Nalla
  • 173
  • 2
  • 15
  • 1
    First of all, not recommended to use too many parameters in same method. See https://rules.sonarsource.com/java/RSPEC-3553, also you can use builder pattern - https://stackoverflow.com/questions/4272756/handling-more-than-7-parameters, – TechFree Dec 16 '19 at 13:07
  • How to fit builder pattern for testng? Example please.. – Sandeep Nalla Dec 17 '19 at 07:16

3 Answers3

1

Your choices are:

1) Disable the check and not validate the number of parameters in methods.

2) Increase max for the check thereby allowing you to have all methods with the new limit.

3) Suppress the violation with a filter for this one location. https://checkstyle.org/config_filters.html . If you want to target methods that use Optional, then I would try to use https://checkstyle.org/config_filters.html#SuppressionXpathFilter .

rveach
  • 2,081
  • 15
  • 25
0

You could create a wrapper class which holds your parameters.

Wrapper class:

public class ParameterObject {
    private String test1;
    private String test2;
    private String test3;
    private String test4;
    private String test5;
    private String test6;
    private String test7;
    private String test8;

    public ParameterObject() {
       super();
    }

    public void setTest1(String test1) {
        this.test1 = test1;
    }

    public String getTest1() {
        return test1;
    }

    //add more getter and setter.
}

Test method:

public void beforeTest(ParameterObject parameters) {
    //access parameters with:
    parameters.getTest1();
    //...
}

You can also use the builder pattern with this wrapper class.

AndiCover
  • 1,724
  • 3
  • 17
  • 38
0

You could use varargs. That allows you to specify an array as a parameter including none.

Linus Fernandes
  • 498
  • 5
  • 30