0

Hello I want to use method response in my junit test method but I have an error: cannot resolve symbol 'response'

What is a reason or maybe u know other method which can I replace in this place?

This is my method:

@PostMapping("/addPeopleToTeams/{teamId}/{personId}")
@ResponseBody
public ResponseEntity<?> addPeopleToTeam(@PathVariable Long teamId, @PathVariable Long personId) {
    TeamsAndPersonsId teamsAndPersonsId = new TeamsAndPersonsId(personId, teamId);
    teamService.findTeamById(teamsAndPersonsId.getTeamId());
    personService.findById(teamsAndPersonsId.getPersonId());
    teamService.addPersonsToTeams(personId, teamId);
    return ResponseEntity.ok(teamsAndPersonsId);

}

And here is my test code:

@Test
    public void shouldAddPersonToTeam() throws Exception {
        // Given
        TeamDto teamDto = prepareTeamDto();
        PersonDto personDto = preparePersonDto();
        // When
        when(teamService.createTeam(teamDto)).thenReturn(Response.ok().build());
        when(personService.addPerson(personDto)).thenReturn(Response.ok().build());
        // than
        mockMvc.perform(post("/addPeopleToTeams/{teamId}/{personId}", 1, 1))
                .andExpect(status().isOk());

    }

This element (Reponse.ok().build()); after thenReturn deosn't work.

1 Answers1

0

I am guessing that import of Response is NOT correct.

On a separate note, production class is using ResponseEntity.ok(teamsAndPersonsId) but your unit test case is Mocking Response class. Make sure that you are using mock object of the same class in your unit test case.

Refer How to unit test a Spring MVC controller using @PathVariable? for a sample test.

gargkshitiz
  • 2,130
  • 17
  • 19