Created
May 31, 2018 12:08
-
-
Save gervaisb/ad71d51fbd81d5b477c2da82cba4a8f3 to your computer and use it in GitHub Desktop.
A couple of uitility methods to create objects in tests
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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