Skip to content

Instantly share code, notes, and snippets.

@gervaisb
Created May 31, 2018 12:08
Show Gist options
  • Save gervaisb/ad71d51fbd81d5b477c2da82cba4a8f3 to your computer and use it in GitHub Desktop.
Save gervaisb/ad71d51fbd81d5b477c2da82cba4a8f3 to your computer and use it in GitHub Desktop.
A couple of uitility methods to create objects in tests
public class Factories {
private Factories() { /* Prevent instantiation */ }
public static <T> List<T> listOf(T... elements) {
return Arrays.asList(elements);
}
public static <T> List<T> listOf(int howMuch, Supplier<T> supplier) {
return listOf(howMuch, (Integer) -> supplier.get());
}
public static <T> List<T> listOf(int howMuch, Function<Integer, T> producer) {
List<T> list = new ArrayList<>(howMuch);
for (int i = 0; i < howMuch; i++) {
list.add(producer.apply(i + 1));
}
return list;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment