[英]Optimize a keyword
I automate tests using SilkTest and Java. In this keyword, I get the list and I compare it to the one expected.我使用 SilkTest 和 Java 自动执行测试。在这个关键字中,我得到了列表并将其与预期的进行比较。 Is there a way to optimize my code since I have declared each list multiple times.
有没有办法优化我的代码,因为我已经多次声明了每个列表。
public void vérification_des_listes(String listAttendu) {
final String[] list1 = listAttendu.split(";", -1);
final ArrayList<String> listExpected = new ArrayList<>();
for (final String I : list1) {
listExpected.add(I);
}
System.out.println(listExpected.toString());
final Object[] list2 = desktop.<DomListBox>find("BrowserApplication.BrowserWindow.Personne.Titre_champ").getItems().toArray();
final ArrayList<String> listFound = new ArrayList<>();
for (final Object E : list2) {
listFound.add(E.toString());
}
System.out.println(listFound.toString());
assertTrue("", listExpected.equals(listFound));
}
Instead of代替
final String[] list1 = listAttendu.split(";", -1);
final ArrayList<String> listExpected = new ArrayList<>();
for (final String I : list1) {
listExpected.add(I);
}
you could use你可以使用
final String[] list1 = listAttendu.split(";", -1);
final List<String> listExpected = Arrays.asList(list1);
See https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/util/Arrays.html#asList(T...)请参阅https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/util/Arrays.html#asList(T...)
public static void vérification_des_listes(String listAttendu) {
final String[] list1 = listAttendu.split(";", -1);
final List<String> listExpected = Arrays.asList(list1);
final Object[] list2 = desktop.<DomListBox>find("BrowserApplication.BrowserWindow.Personne.Titre_champ").getItems().toArray();
final List<String> listFound = Arrays.stream(list2).map(Objects::toString).toList();
Assertions.assertEquals(listExpected, listFound);
}
You can take advantage of class Arrays, and also do some java stream processing for converting your objects to String.您可以利用 class Arrays,也可以进行一些 java stream 处理,将您的对象转换为字符串。 Also take a look at assertj to compare Collections in a more flexible way (can see an example here ).
还可以查看 assertj 以更灵活的方式比较 Collections(可以在此处查看示例)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.