Search code examples
javaandroidunit-testingassert

Unit Testing Android function


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

}

Solution

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

    Or, if you want exactly arraylist, just

    items = new ArrayList<>();
    items.add("strawberry");
    items.add("raspberry");