본문 바로가기
반응형
Test Code/Test Code(eng)

AppointmentQueryService Class - Test Code

by brightGarden02 2022. 11. 10.

I wrote test code per one class.

I chose AppointmentQueryService to write test code.

Before wrtting test code, I put data in database.

 

I classified into 3 categories in one method.

"given, when, then"

 

given: something is given.

when: something is done.

then: verification

 

@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: Doctor is given

when: bring List<Appointment>

then: verifiy appointment.size() is 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);
}

The difference between upper code is that using 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);
}

Same contents.

But used 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: Appointment is given

when: use ResponseDto in appointment

then: verify result.getContent() is equal to "기침을 많이 합니다"

 

 

 

@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: Appointment is given

when: bring appointmentModifyDto through appointmentQueryService.findAppointmentModifyDtoById()

then: verify hour, minute is equal to 1, 52

 

 

 

@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: Doctor, Appointment are given

when: bring List<LocalDateTime> using appointmentQueryService.findDateTImesByDateAndDoctor()

then: verify reserved hour, minute is equal to 10, 30

 

 

 

When dividing into "given" and "when", it could be ambiguous.

I chose that the one closest to the verification is set to "when".

'Test Code > Test Code(eng)' 카테고리의 다른 글

AppointmentCommandService Class - Test Code  (0) 2022.11.11

댓글


반응형
반응형