我想在我的代码中对一个函数进行单元测试,但是当我尝试向其中输入一个 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");
或者,如果你想要 arraylist,就
items = new ArrayList<>();
items.add("strawberry");
items.add("raspberry");
关于java - 单元测试Android功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49158665/