1
resposta

Erro no teste do controller

@Test
    @DisplayName("Should return HTTP code 200 when the data is correct")
    @WithMockUser
    public void scheduleConsultationScenery2() throws Exception{
        var date = LocalDateTime.now().plusHours(2);
        var specialty = Specialty.CARDIOLOGIA;

        var consultationDetails = new ConsultationDataDetailsDTO(null, 2l, 5l, date);

        when(consultationService.scheduleConsultation(any())).thenReturn(consultationDetails);

        var response = mockMvc.perform(post("/consultation/schedule")
                .contentType(MediaType.APPLICATION_JSON)
                .content(jacksonTesterScheduleJson.write(
                        new ConsultationScheduleDataDTO(2l, 5l, date, specialty)
                ).getJson())
                )
                .andReturn().getResponse();

        assertThat(response.getStatus()).isEqualTo(HttpStatus.OK.value());

        var expectedJson = jacksonTesterReturnJson.write(
            consultationDetails
        ).getJson();

        assertThat(response.getContentAsString()).isEqualTo(expectedJson);

    }

Linha: when(consultationService.scheduleConsultation(any())).thenReturn(consultationDetails);

Erro:

Cannot resolve method 'thenReturn(ConsultationDataDetailsDTO)'
ConsultationDataDetailsDTO consultationDetails
    = new ConsultationDataDetailsDTO(null, 2l, 5l, date)

java: no suitable method found for thenReturn(clin.dan.api.Features.Consultation.ConsultationDTOs.ConsultationDataDetailsDTO)
    method org.mockito.stubbing.OngoingStubbing.thenReturn(org.springframework.http.ResponseEntity<clin.dan.api.Features.Consultation.ConsultationDTOs.ConsultationDataDetailsDTO>) is not applicable
      (argument mismatch; clin.dan.api.Features.Consultation.ConsultationDTOs.ConsultationDataDetailsDTO cannot be converted to org.springframework.http.ResponseEntity<clin.dan.api.Features.Consultation.ConsultationDTOs.ConsultationDataDetailsDTO>)
    method org.mockito.stubbing.OngoingStubbing.thenReturn(org.springframework.http.ResponseEntity<clin.dan.api.Features.Consultation.ConsultationDTOs.ConsultationDataDetailsDTO>,org.springframework.http.ResponseEntity<clin.dan.api.Features.Consultation.ConsultationDTOs.ConsultationDataDetailsDTO>...) is not applicable
      (argument mismatch; clin.dan.api.Features.Consultation.ConsultationDTOs.ConsultationDataDetailsDTO cannot be converted to org.springframework.http.ResponseEntity<clin.dan.api.Features.Consultation.ConsultationDTOs.ConsultationDataDetailsDTO>)

https://github.com/danilobsilv/Clin

1 resposta

Oi!

Seu método scheduleConsultation, da classe ConsultationService, não tem como retorno um objeto ConsultationDataDetailsDTO, mas sim um objeto do tipo ResponseEntity<ConsultationDataDetailsDTO>.

Por isso está dando erro na linha que você indicou, pois você está configurando o mock para retornar um tipo incompatível com o retorno do método.