[英]Unit Testing Android function
我想在我的代碼中對一個函數進行單元測試,但是當我嘗試向其中輸入一個 ArrayList 時,它給了我array initializer not allowed here
這是我的代碼:
public class CreateActivityTest {
ArrayList<String> items;
public String Add(String item, ArrayList<String> items) {
if (items.contains(item.toLowerCase().trim())) {
return null;
}
else if (item == null || item.trim().equals("")) {
return null;
} else {
return item.toLowerCase().replaceAll("\\s+", "");
}
}
@Test
public void addTest () throws Exception {
items = {"strawberry", "raspberry"};
String input = Add("Strawberry ", items);
String expected = null;
assertEquals(input, expected);
}
}
items = Arrays.asList("strawberry", "raspberry");
或者,如果您想要確切的數組列表,只需
items = new ArrayList<>();
items.add("strawberry");
items.add("raspberry");
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.