![](/img/trans.png)
[英]Esper: Notice, when number of events (with specific parameters) exceeds a threshold
[英]TestNG - How to avoid checkstyle error when number of parameters exceeds 7?
当一个方法的参数数量超过 7 时,会抛出 checkstyle 错误(即超过 7 个参数(找到 8 个)。[ParameterNumber])。 因此,对于下面的方法也是抛出。 通常,使用 String 数组或 Hashmap 可以避免 checkstyle 错误。
但是,如何避免在这里成为具有 @Optional 注释的方法参数?
@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) {
....
}
一种方法是增加 checkstyle.xml 中的参数限制。
但是,寻找是否有更好的解决方案。
您的选择是:
1) 禁用检查并且不验证方法中的参数数量。
2)增加检查的max
,从而允许您拥有具有新限制的所有方法。
3) 用过滤器抑制这一位置的违规。 https://checkstyle.org/config_filters.html 。 如果您想定位使用Optional
方法,那么我会尝试使用https://checkstyle.org/config_filters.html#SuppressionXpathFilter 。
您可以创建一个包装类来保存您的参数。
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.
}
public void beforeTest(ParameterObject parameters) {
//access parameters with:
parameters.getTest1();
//...
}
您还可以将构建器模式与此包装器类一起使用。
你可以使用可变参数。 这允许您指定一个数组作为参数,包括无。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.