GameSpecSourceInstantiator { public List<GameSpecSource> instantiateGameSpecSources(List<Class> gamesClasses) { return gamesClasses .stream() .map(gameClass -> { try { Constructor<GameSpecSource> defaultConstructor = gameClass.getConstructor(); return defaultConstructor.newInstance(); } catch ( NoSuchMethodException | IllegalAccessException | InstantiationException | InvocationTargetException | ClassCastException e) { throw new GameSpecInstantiationException("Failed to create new instance of class " + gameClass, e); } }) .collect(Collectors.toList()); }  List<GameSpecSource> instantiateGameSpecSources(List<Class> gamesClasses);  }
@Test void shouldInstantiateFromListOfSources() throws ClassNotFoundException { List<Class> classList = new ArrayList<>(List.of( Class.forName("net.gazeplay.games.videogrid.VideoGridGameSpecSource"), Class.forName("net.gazeplay.games.order.OrderGameSpecSource"), Class.forName("net.gazeplay.games.mediaPlayer.MediaPlayerGameSpecSource") )); GameSpecSourceInstantiator instantiator = new GameSpecSourceInstantiator(); List<GameSpecSource> result = instantiator.instantiateGameSpecSources(classList); assertEquals(3, result.size()); }
@Test void shouldInstantiateEmptyListFromEmptyListOfSources() { List<Class> classList = new ArrayList<>(); GameSpecSourceInstantiator instantiator = new GameSpecSourceInstantiator(); List<GameSpecSource> result = instantiator.instantiateGameSpecSources(classList); assertEquals(0, result.size()); }
@Test void shouldThrowExceptionForIncorrectClass() { assertThrows(GameSpecSourceInstantiator.GameSpecInstantiationException.class, () -> { List<Class> classList = new ArrayList<>(List.of( Class.forName("net.gazeplay.commons.utils.FileUtils") )); GameSpecSourceInstantiator instantiator = new GameSpecSourceInstantiator(); instantiator.instantiateGameSpecSources(classList); }); }