This is the method in my service. I get the Exception when .stream.map.collect
@Override
public GameDto save(GameRequestDto gameRequestDto) {
Optional<Game> gameOptional = gameRepository.findByTitleAndStudio(gameRequestDto.getTitle(),
gameRequestDto.getStudio());
if (gameOptional.isPresent()) {
throw new InvalidRequestException("Game with title: " + gameRequestDto.getTitle() +
" and studio: " + gameRequestDto.getStudio() + " already exists.");
}
Game gameToBeSaved = buildGameToBeSaved(gameRequestDto);
Game savedGame = gameRepository.save(gameToBeSaved);
GameDto gameDto = gameMapper.gameToGameDto(savedGame);
gameDto.setGameGenres(savedGame.getGenres().stream()
.map(Genre::getGenreName)
.collect(Collectors.toCollection(HashSet::new)));
return gameDto;
}
This is my Test
@InjectMocks
GameServiceImpl gameService;
@Mock
GameRepository gameRepository;
@Mock
GenreRepository genreRepository;
@Test
public void saveGameTest() throws Exception {
Set genres = new HashSet<>();
genres.add("Role Playing");
Game savedGame = buildGame(genres);
Mockito.when(gameRepository.save(any(Game.class))).thenReturn(savedGame);
gameService.save(buildGameRequestDto(genres));
Mockito.verify(genreRepository, Mockito.times(4));
Mockito.verify(gameRepository, Mockito.times(1));
}
And this is the Exception
java.lang.ClassCastException: class java.lang.String cannot be cast to class com.home.myproject.entities.Genre (java.lang.String is in module java.base of loader 'bootstrap'; com.home.myproject.entities.Genre is in unnamed module of loader 'app')