1

I am trying to pass a collection of string values from a properties file to a parameterized JUnit test. Properties.values() returns Collection while JUnit requires the parameters be passed in Collection structure.

Does that mean I have to convert Collection<Object> to Collection<Object[]>, where each array is actually a single item?

I tried this:

Arrays.asList(new Object[][] {{theProperties.values()}});

But this puts all the values together in one Object and does not create a Collection as I expect it to. Can someone please help?

techjourneyman
  • 1,701
  • 3
  • 33
  • 53

1 Answers1

4

Looks like parameterized JUnit tests requires a Collection even if each test has a single parameter.

Converting a Collection to Collection:

Using Java 8:

Collection<String> c = Arrays.asList("a", "b");
Collection<Object[]> co = c.stream()
                            .map(ele -> new Object[]{ele})
                            .collect(Collectors.toList());

Using Java 7 or below:

Collection<String> coll = Arrays.asList("a", "b");
Collection<Object[]> params = new ArrayList<Object[]>();
for (String s : coll) {
    params.add(new Object[] { s });
}

For Java 7 or below, you can either:

nhylated
  • 1,575
  • 13
  • 19