阅读Java TestNG测试顶部的注释,我的注释为:
@TestInfo(id={ " C26603", " C10047" })
其中TestInfo
只是具有id() as String array
:的接口
public String[] id() default {};
而CCD_ 3和CCD_。
以下是测试结构的样子(例如):
案例1:
@TestInfo(id={ " C26603", " C10047" })
public void testDoSomething() {
Assert.assertTrue(false);
}
同样,更干净的情况是:
情况2:
@TestInfo(id={ "C26603", "C10047" })
正如你所看到的,这种情况2比情况1更清楚。这种情况2在测试id中没有空格
如何获取这些id,并确保它们的开头没有C字符,只有一个纯数字例如,我只想要26603
作为我的第一个id,10047
作为第二个id。id数组中有一些空格(引号内)。我想修剪所有这些(比如空白),只得到id。我目前正在应用for loop
来处理每个id,一旦我得到了纯数字,我想进行第三方API调用(API希望纯数字作为输入,因此删除C作为初始字符和其他空白很重要)。
以下是我尝试过的:
TestInfo annotation = method.getAnnotation(TestInfo.class);
if(annotation!=null) {
for(String test_id: annotation.id()) {
//check if id is null or empty
if (test_id !=null && !test_id.isEmpty()) {
//remove white spaces and check if id = "C1234" or id = "1234"
if(Character.isLetter(test_id.trim().charAt(0))) {
test_id = test_id.substring(1);
}
System.out.println(test_id);
System.out.println(test_id.trim());
}
}
}
上面的代码给出了情况1的C26603
和not 26603
。它适用于案例2。
情况3:
@TestInfo(id={ " 26603", " 10047" })
对于这种情况,没有C作为测试id的起始字符,所以函数应该足够聪明,只需要修剪空白并继续。
最简单的方法是使用正则表达式非数字字符类(D
):删除所有不是数字的内容
test_id = test_id.replaceAll("\D", "");
我强烈建议您调试您的方法。你会学到很多东西。
如果您在此处查看您的if
声明:
if(Character.isLetter(test_id.trim().charAt(0))) {
test_id = test_id.substring(1);
}
当您的test_id
="C1234"时,您的条件成立。但是,您的问题变成了substring
。
答案:trim
!
test_id = test_id.trim().substring(1);