2

I want to unit test a function within my code, but when I try to input a ArrayList into it it gives me array initializer not allowed here

here is my code:

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);
}

}
Kyril Khaltesky
  • 85
  • 1
  • 1
  • 7
  • `items = {"strawberry", "raspberry"};` is not valid java in this context, use `new ArrayList<>(...)`. – luk2302 Mar 07 '18 at 18:13
  • `items = new ArrayList("strawberry", "raspberry")` this doesnt work either, or am i doing it wrong? – Kyril Khaltesky Mar 07 '18 at 18:16
  • 2
    Possible duplicate of [How to declare an ArrayList with values?](https://stackoverflow.com/questions/21696784/how-to-declare-an-arraylist-with-values) – luk2302 Mar 07 '18 at 18:17

1 Answers1

1
items = Arrays.asList("strawberry", "raspberry");

Or, if you want exactly arraylist, just

items = new ArrayList<>();
items.add("strawberry");
items.add("raspberry");
nyarian
  • 4,085
  • 1
  • 19
  • 51