Convenient List Generation with java.util.stream.IntStream 📎
List
of classes or records containing an index / number for test purposes.
The following snippet generates five ints, instantiates a record
instance with the generated number and finally
converts the stream into a List<Developer>
:
import java.util.stream.IntStream;
import org.junit.jupiter.api.Test;
public class StreamGenerationTest {
@Test
public void listGeneration() {
record Developer(int id) {
}
var developerList = IntStream.range(0, 5)
.mapToObj(Developer::new)
.toList();
assertEquals(5, developerList.size());
System.out.println(developerList);
}
}
The output: [Developer[id=0], Developer[id=1], Developer[id=2], Developer[id=3], Developer[id=4]]
Big thanks to @ProjectPolly for the IntStream hint.