단일 클래스에 대해 짰으며
AppointmentQueryService 클래스에 대해 테스트 코드를 짜보았다.
데이터를 디비에 이미 넣어둔 상태로 테스트 코드를 작성을 진행하였다.
테스트코드는 given, when, then으로 나누어 작성하였다.
given: 00이 주어짐
when: 00한 경우
then: 검증
@Test
void findAllAppointmentEntityListTest() throws Exception {
    //given
    Doctor doctor = doctorRepository.findById(5L).get();
    //when
    List<Appointment> appointments = appointmentRepository.findByDoctorId(doctor.getId());
    //then
    assertThat(appointments.size()).isEqualTo(9);
}given: 닥터가 주어짐
when: 닥터로 예약목록을 가져온 경우
then: 현재 닥터에 9개의 예약목록이 있기에 9개와 같은지 검증
@Test
void findAllAppointmentDtoListTest() throws Exception {
    //given
    Doctor doctor = doctorRepository.findById(5L).get();
    //when
    List<Appointment> appointments = appointmentRepository.findByDoctorId(doctor.getId());
    List<AppointmentDto> appointmentDtos = appointments
            .stream()
            .map(o -> new AppointmentDto(o))
            .collect(Collectors.toList());
    //then
    assertThat(appointmentDtos.size()).isEqualTo(9);
}위 코드와 차이점은 예약목록을 DTO로 감샀을 때의 검증이다.
@Test
void findAllAppointmentTest() throws Exception {
    //given
    Doctor doctor = doctorRepository.findById(5L).get();
    //when
    List<AppointmentDto> appointmentDtos = appointmentQueryService.findAllAppointment(doctor.getEmail());
    //then
    assertThat(appointmentDtos.size()).isEqualTo(9);
}위 코드와 내용은 같으며
appointmentQueryService.findAllAppointment()를 사용한 테스트코드이다.
@Test
void findAppointmentDtoByIdTest() throws Exception {
    //given
    Appointment appointment = appointmentRepository.findByAppointmentId(1L).get();
    //when
    LoadMyPageDoctorAppointment.ResponseDto result = new LoadMyPageDoctorAppointment.ResponseDto(appointment);
    //then
    assertThat(result.getContent()).isEqualTo("기침을 많이 합니다");
}given: 예약이 주어짐
when: 예약을 ResponseDto로 감싼 경우
then: result의 내용이 같은지 검증
@Test
void findAppointmentModifyDtoByIdTest() throws Exception {
    //given
    Appointment appointment = appointmentRepository.findByAppointmentId(2L).get();
    //when
    AppointmentModifyDto appointmentModifyDto = appointmentQueryService.findAppointmentModifyDtoById(appointment.getId());
    //then
    assertThat(appointmentModifyDto.getDate().getHour()).isEqualTo(1);
    assertThat(appointmentModifyDto.getDate().getMinute()).isEqualTo(52);
}given: 예약이 주어짐
when: appointmentModifyDto를 가져온 경우
then: 시, 분이 같은지 검증
@Test
void findDateTimesByDateAndDoctorTest() throws Exception {
    //given
    Doctor doctor = doctorRepository.findById(6L).get();
    Appointment appointment = appointmentRepository.findById(1L).get();
    LocalDateTime date = appointment.getDate();
    LocalDate localDate = date.toLocalDate();
    //when
    List<LocalDateTime> result = appointmentQueryService.findDateTimesByDateAndDoctor(localDate, doctor.getId());
    //then
    assertThat(result.get(0).getHour()).isEqualTo(10);
    assertThat(result.get(0).getMinute()).isEqualTo(30);
}given: 닥터, 예약이 주어짐
when: 시간, 닥터Id를 통해 예약시간을 가져온 상황
then: 예약한 시, 분이 같은지 검증
'Test Code' 카테고리의 다른 글
| AppointmenCommandService 클래스 - 테스트 코드 (0) | 2022.11.11 | 
|---|
댓글