-1

So I've got a controller class and want to test it, but don't know how. I have been trying to use MockMvc but don't know how to actually pass the query parameters. My code is as follows:

public class AppointmentController {

@GetMapping(path = "/getAppointmentById")
    @ApiOperation(value = "Fetch An Existing Appointment", notes = "Provide Date In Range (From-To {Date})")
    public ResponseEntity<AppointmentDto> getAppointmentById(

            @ApiParam(value = "Appointment Id", required = true) @RequestParam("id") Long id) {
        try {

            log.info("Get AppointmentById");
            AppointmentDto appointmentDto = appointmentService.getAppointmentById(id);
            log.info("Sending response getAppointmentById ");
            return new ResponseEntity<>(appointmentDto, HttpStatus.OK);
        } catch (Exception e) {
            log.error("Exception happen" + e.getMessage());
            return new ResponseEntity<>(null, HttpStatus.INTERNAL_SERVER_ERROR);
        }

    }

    @GetMapping(path = "/getAppointmentToPatient")
    @ApiOperation(value = "Fetch Patient Appointment", notes = "Provide Date In Range (From-To)")
    public ResponseEntity<List<Appointment>> getAppointmentToPatient(
            @ApiParam(value = "Patient Id", required = true) @RequestParam Long patientId,
            @ApiParam(value = "Start Date", required = true) @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate startDate,
            @ApiParam(value = "End Date", required = true) @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate endDate) {

        try {
            log.info("Getting all appointment of patient from {Startdate} - {Enddate}");

            List<Appointment> listAppointment = appointmentService.getAppointmentToPatient(patientId, startDate,
                    endDate);
            log.info("Sending response getAppointmentToPatient ");
            return new ResponseEntity<>(listAppointment, HttpStatus.OK);
        } catch (Exception e) {
            log.error("Exception happen" + e.getMessage());
            return new ResponseEntity<>(null, HttpStatus.INTERNAL_SERVER_ERROR);
        }

    }
}
Gregor Zurowski
  • 2,222
  • 2
  • 9
  • 15

1 Answers1

0

Both of your endpoints accept GET requests, therefore you will only have to pass in query parameters that can done with the param method:

mockMvc.perform(
   get("/getAppointmentById")
      .param("id", "1")
   ).andDo(print())
      .andExpect(status().isOk());

Following is the example for the second endpoint:

mockMvc.perform(
   get("/getAppointmentToPatient")
      .param("patientId", "2")
      .param("startDate", "2021-10-11")
      .param("endDate", "2021-10-12")
   ).andDo(print())
      .andExpect(status().isOk());
Gregor Zurowski
  • 2,222
  • 2
  • 9
  • 15