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);
}
}
}