### Question:
ContenderController { @RequestMapping(value = "/game/{gameId}/name/{name}/vote", method = RequestMethod.POST) public void vote(@PathVariable String name, @PathVariable Long gameId, @RequestBody VoteRequest voteRequest ) throws Exception { Long questionAnswerId = voteRequest.getQuestionAnswerId(); Game game = repoUtil.findGameById(gameId); QuestionAnswer questionAnswer = repoUtil.findQuestionAnswerById(questionAnswerId); boolean validQuestionAnswerId = false; for (QuestionAnswer questionAnswerGame: game.getCurrentQuestionAnswers()) { if (questionAnswerGame.getId().equals(questionAnswerId)){ validQuestionAnswerId = true; break; } } if (!validQuestionAnswerId) { throw new Exception("You tried to cheat, that isn't a current question to vote on!"); } LOGGER.info(String.format("%s voted for id: %d", name, questionAnswerId)); questionAnswer.incrementScore(); questionAnswerRepository.save(questionAnswer); updateQuestions(questionAnswer); }  @RequestMapping(value = "/game/{gameId}/name/{name}/questions", method = RequestMethod.GET) @ResponseBody QuestionsResponse restGetQuestions(@PathVariable Long gameId, @PathVariable String name); @RequestMapping(value = "/game/{gameId}/name/{name}/answer", method = RequestMethod.POST) void submitAnswer(@PathVariable String name,
                             @PathVariable Long gameId,
                             @RequestBody AnswerRequest answerRequest); @RequestMapping(value = "/game/{gameId}/name/{name}/vote", method = RequestMethod.POST) void vote(@PathVariable String name,
                     @PathVariable Long gameId,
                     @RequestBody VoteRequest voteRequest
    );  }### Answer:
@Test public void givenValidNameAndGameIdAndVoteRequest_whenCallingVote_thenSaveVote() throws Exception { QuestionAnswer questionAnswer = new QuestionAnswer(); questionAnswer.setId(1L); Contender contender = new Contender(); contender.setScore(0L); questionAnswer.setContender(contender); Game game = new Game(); game.setCurrentQuestionAnswers(Collections.singletonList(questionAnswer)); VoteRequest voteRequest = new VoteRequest(); voteRequest.setQuestionAnswerId(1L); doReturn(game).when(repoUtil).findGameById(1L); doReturn(questionAnswer).when(repoUtil).findQuestionAnswerById(1L); mockMvc.perform(post("/game/1/name/name/vote").contentType(MediaType.APPLICATION_JSON) .content("{\"questionAnswerId\": 1}")) .andExpect(status().isOk()); contenderControllerMock.vote("name", 1L, voteRequest); verify(questionAnswerRepository, times(2)).save(questionAnswer); }
@Test(expected = Exception.class) public void givenValidNameAndGameIdAndInvalidVoteRequest_whenCallingVote_thenThrowErrorAndDoNotSaveVote() throws Exception { QuestionAnswer questionAnswer = new QuestionAnswer(); questionAnswer.setId(1L); Game game = new Game(); game.setCurrentQuestionAnswers(Collections.singletonList(questionAnswer)); VoteRequest voteRequest = new VoteRequest(); voteRequest.setQuestionAnswerId(2L); doReturn(game).when(repoUtil).findGameById(1L); doReturn(questionAnswer).when(repoUtil).findQuestionAnswerById(1L); mockMvc.perform(post("/game/1/name/name/vote").contentType(MediaType.APPLICATION_JSON) .content("{\"questionAnswerId\": 1}")) .andExpect(status().isOk()); contenderControllerMock.vote("name", 1L, voteRequest); verify(questionAnswerRepository, times(0)).save(questionAnswer); }