79518638

Date: 2025-03-18 21:07:40
Score: 0.5
Natty:
Report link

Yes, use Dtos. Here is a common practice example:

    @Test
    void PersonController_FindPersonById() throws Exception {

        PersonDto personDto = new PersonDto(null, "John", "Doe", 28, null);

        long personId = 1;

        when(personService.getPersonById(personId)).thenReturn(personDto);

        ResultActions response = mockMvc.perform(get("/person/1")
                .contentType(MediaType.APPLICATION_JSON)
                .content(objectMapper.writeValueAsString(personDto))
        );

        response.andExpect(MockMvcResultMatchers.status().isOk())
                .andExpect(MockMvcResultMatchers.jsonPath("$.firstname", CoreMatchers.is(personDto.firstname())))
                .andExpect(MockMvcResultMatchers.jsonPath("$.lastname", CoreMatchers.is(personDto.lastname())))
    }
Reasons:
  • Long answer (-0.5):
  • Has code block (-0.5):
  • Unregistered user (0.5):
  • Low reputation (1):
Posted by: Klaus Weimer