From 8baad04762d805fee75fbcf6910b6fd603823ad7 Mon Sep 17 00:00:00 2001 From: wonu606 Date: Mon, 18 Dec 2023 16:05:36 +0900 Subject: [PATCH 1/6] =?UTF-8?q?test:=20ControllerTest=EC=97=90=EC=84=9C=20?= =?UTF-8?q?=EC=82=AC=EC=9A=A9=ED=95=A0=20RestDocs=20Abstract=20Class=20?= =?UTF-8?q?=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../studayserver/docs/RestDocsSupport.java | 29 +++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 src/test/java/org/guzzing/studayserver/docs/RestDocsSupport.java diff --git a/src/test/java/org/guzzing/studayserver/docs/RestDocsSupport.java b/src/test/java/org/guzzing/studayserver/docs/RestDocsSupport.java new file mode 100644 index 00000000..d06e9ec8 --- /dev/null +++ b/src/test/java/org/guzzing/studayserver/docs/RestDocsSupport.java @@ -0,0 +1,29 @@ +package org.guzzing.studayserver.docs; + +import static org.springframework.restdocs.mockmvc.MockMvcRestDocumentation.documentationConfiguration; + +import com.fasterxml.jackson.databind.ObjectMapper; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.extension.ExtendWith; +import org.springframework.restdocs.RestDocumentationContextProvider; +import org.springframework.restdocs.RestDocumentationExtension; +import org.springframework.test.web.servlet.MockMvc; +import org.springframework.test.web.servlet.setup.MockMvcBuilders; + +@ExtendWith(RestDocumentationExtension.class) +public abstract class RestDocsSupport { + + protected MockMvc mockMvc; + protected ObjectMapper objectMapper; + + @BeforeEach + void setup(RestDocumentationContextProvider provider) { + mockMvc = MockMvcBuilders.standaloneSetup(initController()) + .apply(documentationConfiguration(provider)) + .build(); + + objectMapper = new ObjectMapper(); + } + + protected abstract Object initController(); +} From 998be36af328e05b6f0da6f0ac423deefc5217f3 Mon Sep 17 00:00:00 2001 From: wonu606 Date: Mon, 18 Dec 2023 19:25:10 +0900 Subject: [PATCH 2/6] =?UTF-8?q?refactor:=20AcademyCalendarController=20Req?= =?UTF-8?q?uestDTO=20=EB=A0=88=EC=BD=94=EB=93=9C=EB=A1=9C=20=EB=B3=80?= =?UTF-8?q?=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../request/AcademyCalendarCreateRequest.java | 8 ++++---- .../request/AcademyCalendarUpdateRequest.java | 8 ++++---- .../controller/dto/request/AttendanceDate.java | 16 ++++------------ .../controller/dto/request/LessonTime.java | 16 ++++------------ .../validation/AttendanceDateValidator.java | 4 ++-- .../request/validation/LessonTimeValidator.java | 4 ++-- 6 files changed, 20 insertions(+), 36 deletions(-) diff --git a/src/main/java/org/guzzing/studayserver/domain/calendar/controller/dto/request/AcademyCalendarCreateRequest.java b/src/main/java/org/guzzing/studayserver/domain/calendar/controller/dto/request/AcademyCalendarCreateRequest.java index aab53a5a..278f7455 100644 --- a/src/main/java/org/guzzing/studayserver/domain/calendar/controller/dto/request/AcademyCalendarCreateRequest.java +++ b/src/main/java/org/guzzing/studayserver/domain/calendar/controller/dto/request/AcademyCalendarCreateRequest.java @@ -45,8 +45,8 @@ public static AcademyCalendarCreateParam to(AcademyCalendarCreateRequest request .stream() .map(lesson -> LessonScheduleCreateRequest.to(lesson)) .toList(), - LocalDate.parse(request.attendanceDate().getStartDateOfAttendance()), - LocalDate.parse(request.attendanceDate().getEndDateOfAttendance()), + LocalDate.parse(request.attendanceDate().startDateOfAttendance()), + LocalDate.parse(request.attendanceDate().endDateOfAttendance()), request.isAlarmed, request.childId, request.dashboardId, @@ -67,8 +67,8 @@ public record LessonScheduleCreateRequest( public static LessonScheduleParam to(LessonScheduleCreateRequest request) { return new LessonScheduleParam( DayOfWeek.valueOf(request.dayOfWeek()), - LocalTime.parse(request.lessonTime().getLessonStartTime()), - LocalTime.parse(request.lessonTime().getLessonEndTime()) + LocalTime.parse(request.lessonTime().lessonStartTime()), + LocalTime.parse(request.lessonTime().lessonEndTime()) ); } } diff --git a/src/main/java/org/guzzing/studayserver/domain/calendar/controller/dto/request/AcademyCalendarUpdateRequest.java b/src/main/java/org/guzzing/studayserver/domain/calendar/controller/dto/request/AcademyCalendarUpdateRequest.java index a0ef2de5..4ebb6039 100644 --- a/src/main/java/org/guzzing/studayserver/domain/calendar/controller/dto/request/AcademyCalendarUpdateRequest.java +++ b/src/main/java/org/guzzing/studayserver/domain/calendar/controller/dto/request/AcademyCalendarUpdateRequest.java @@ -48,8 +48,8 @@ public static AcademyCalendarUpdateParam to(AcademyCalendarUpdateRequest request .stream() .map(lesson -> LessonScheduleUpdateRequest.to(lesson)) .toList(), - LocalDate.parse(request.attendanceDate().getStartDateOfAttendance()), - LocalDate.parse(request.attendanceDate().getEndDateOfAttendance()), + LocalDate.parse(request.attendanceDate().startDateOfAttendance()), + LocalDate.parse(request.attendanceDate().endDateOfAttendance()), request.isAlarmed, memberId, request.childId, @@ -72,8 +72,8 @@ public record LessonScheduleUpdateRequest( public static LessonScheduleParam to(LessonScheduleUpdateRequest request) { return new LessonScheduleParam( DayOfWeek.valueOf(request.dayOfWeek()), - LocalTime.parse(request.lessonTime().getLessonStartTime()), - LocalTime.parse(request.lessonTime().getLessonEndTime()) + LocalTime.parse(request.lessonTime().lessonStartTime()), + LocalTime.parse(request.lessonTime().lessonEndTime()) ); } } diff --git a/src/main/java/org/guzzing/studayserver/domain/calendar/controller/dto/request/AttendanceDate.java b/src/main/java/org/guzzing/studayserver/domain/calendar/controller/dto/request/AttendanceDate.java index 746d64af..3081f594 100644 --- a/src/main/java/org/guzzing/studayserver/domain/calendar/controller/dto/request/AttendanceDate.java +++ b/src/main/java/org/guzzing/studayserver/domain/calendar/controller/dto/request/AttendanceDate.java @@ -1,20 +1,12 @@ package org.guzzing.studayserver.domain.calendar.controller.dto.request; +import com.fasterxml.jackson.annotation.JsonProperty; import jakarta.validation.constraints.NotBlank; import lombok.Getter; import org.guzzing.studayserver.domain.calendar.controller.dto.request.validation.ValidAttendanceDate; -@Getter @ValidAttendanceDate -public class AttendanceDate { - - private String startDateOfAttendance; - - private String endDateOfAttendance; - - @NotBlank - public AttendanceDate(String startDateOfAttendance, String endDateOfAttendance) { - this.startDateOfAttendance = startDateOfAttendance; - this.endDateOfAttendance = endDateOfAttendance; - } +public record AttendanceDate( + String startDateOfAttendance, + String endDateOfAttendance) { } diff --git a/src/main/java/org/guzzing/studayserver/domain/calendar/controller/dto/request/LessonTime.java b/src/main/java/org/guzzing/studayserver/domain/calendar/controller/dto/request/LessonTime.java index 6dabe510..02800bc9 100644 --- a/src/main/java/org/guzzing/studayserver/domain/calendar/controller/dto/request/LessonTime.java +++ b/src/main/java/org/guzzing/studayserver/domain/calendar/controller/dto/request/LessonTime.java @@ -1,20 +1,12 @@ package org.guzzing.studayserver.domain.calendar.controller.dto.request; +import com.fasterxml.jackson.annotation.JsonProperty; import jakarta.validation.constraints.NotBlank; import lombok.Getter; import org.guzzing.studayserver.domain.calendar.controller.dto.request.validation.ValidLessonTime; -@Getter @ValidLessonTime -public class LessonTime { - - private String lessonStartTime; - - private String lessonEndTime; - - @NotBlank - public LessonTime(String lessonStartTime, String lessonEndTime) { - this.lessonStartTime = lessonStartTime; - this.lessonEndTime = lessonEndTime; - } +public record LessonTime( + String lessonStartTime, + String lessonEndTime) { } diff --git a/src/main/java/org/guzzing/studayserver/domain/calendar/controller/dto/request/validation/AttendanceDateValidator.java b/src/main/java/org/guzzing/studayserver/domain/calendar/controller/dto/request/validation/AttendanceDateValidator.java index 8c1f179c..41627b0e 100644 --- a/src/main/java/org/guzzing/studayserver/domain/calendar/controller/dto/request/validation/AttendanceDateValidator.java +++ b/src/main/java/org/guzzing/studayserver/domain/calendar/controller/dto/request/validation/AttendanceDateValidator.java @@ -21,8 +21,8 @@ public boolean isValid(AttendanceDate attendanceDate, ConstraintValidatorContext } try { - LocalDate startDate = LocalDate.parse(attendanceDate.getStartDateOfAttendance()); - LocalDate endDate = LocalDate.parse(attendanceDate.getEndDateOfAttendance()); + LocalDate startDate = LocalDate.parse(attendanceDate.startDateOfAttendance()); + LocalDate endDate = LocalDate.parse(attendanceDate.endDateOfAttendance()); if (endDate.isAfter(startDate.plusYears(MAX_DIFFERENCE_YEAR)) || endDate.isBefore(startDate)) { return false; diff --git a/src/main/java/org/guzzing/studayserver/domain/calendar/controller/dto/request/validation/LessonTimeValidator.java b/src/main/java/org/guzzing/studayserver/domain/calendar/controller/dto/request/validation/LessonTimeValidator.java index 0faa9aff..03e13d66 100644 --- a/src/main/java/org/guzzing/studayserver/domain/calendar/controller/dto/request/validation/LessonTimeValidator.java +++ b/src/main/java/org/guzzing/studayserver/domain/calendar/controller/dto/request/validation/LessonTimeValidator.java @@ -26,8 +26,8 @@ public boolean isValid(LessonTime lessonTime, ConstraintValidatorContext context } try { - LocalTime startTime = LocalTime.parse(lessonTime.getLessonStartTime(), TIME_FORMATTER); - LocalTime endTime = LocalTime.parse(lessonTime.getLessonEndTime(), TIME_FORMATTER); + LocalTime startTime = LocalTime.parse(lessonTime.lessonStartTime(), TIME_FORMATTER); + LocalTime endTime = LocalTime.parse(lessonTime.lessonEndTime(), TIME_FORMATTER); if (!isValidTimeRange(startTime, endTime, context) || !isStartTimeBeforeEndTime(startTime, endTime, context)) { From f2c1bd37c575538934b46eedc2172eac9be83cac Mon Sep 17 00:00:00 2001 From: wonu606 Date: Mon, 18 Dec 2023 19:34:37 +0900 Subject: [PATCH 3/6] =?UTF-8?q?test:=20AcademyCalendarControllerTest=20Res?= =?UTF-8?q?tDocsSupport=20=EC=83=81=EC=86=8D=EB=B0=9B=EC=95=84=20=ED=85=8C?= =?UTF-8?q?=EC=8A=A4=ED=8A=B8=20=EC=88=98=ED=96=89=ED=95=98=EB=8F=84?= =?UTF-8?q?=EB=A1=9D=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../studayserver/docs/RestDocsSupport.java | 6 +++ .../AcademyCalendarControllerTest.java | 40 +++++++------------ 2 files changed, 20 insertions(+), 26 deletions(-) diff --git a/src/test/java/org/guzzing/studayserver/docs/RestDocsSupport.java b/src/test/java/org/guzzing/studayserver/docs/RestDocsSupport.java index d06e9ec8..e63a9018 100644 --- a/src/test/java/org/guzzing/studayserver/docs/RestDocsSupport.java +++ b/src/test/java/org/guzzing/studayserver/docs/RestDocsSupport.java @@ -1,10 +1,16 @@ package org.guzzing.studayserver.docs; +import static org.mockito.BDDMockito.given; import static org.springframework.restdocs.mockmvc.MockMvcRestDocumentation.documentationConfiguration; import com.fasterxml.jackson.databind.ObjectMapper; +import org.guzzing.studayserver.domain.calendar.facade.AcademyCalendarFacade; +import org.guzzing.studayserver.domain.calendar.service.AcademyCalendarService; +import org.guzzing.studayserver.domain.child.service.ChildFacade; +import org.guzzing.studayserver.domain.child.service.ChildService; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.extension.ExtendWith; +import org.springframework.boot.test.mock.mockito.MockBean; import org.springframework.restdocs.RestDocumentationContextProvider; import org.springframework.restdocs.RestDocumentationExtension; import org.springframework.test.web.servlet.MockMvc; diff --git a/src/test/java/org/guzzing/studayserver/domain/calendar/controller/AcademyCalendarControllerTest.java b/src/test/java/org/guzzing/studayserver/domain/calendar/controller/AcademyCalendarControllerTest.java index aea315b6..4e234441 100644 --- a/src/test/java/org/guzzing/studayserver/domain/calendar/controller/AcademyCalendarControllerTest.java +++ b/src/test/java/org/guzzing/studayserver/domain/calendar/controller/AcademyCalendarControllerTest.java @@ -1,14 +1,14 @@ package org.guzzing.studayserver.domain.calendar.controller; +import static org.mockito.Mockito.mock; import static org.springframework.restdocs.mockmvc.RestDocumentationRequestBuilders.put; -import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.csrf; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; -import com.fasterxml.jackson.databind.ObjectMapper; import java.time.DayOfWeek; import java.util.List; import java.util.stream.Stream; +import org.guzzing.studayserver.docs.RestDocsSupport; import org.guzzing.studayserver.domain.calendar.controller.dto.request.AcademyCalendarCreateRequest; import org.guzzing.studayserver.domain.calendar.controller.dto.request.AcademyCalendarUpdateRequest; import org.guzzing.studayserver.domain.calendar.controller.dto.request.AttendanceDate; @@ -16,59 +16,47 @@ import org.guzzing.studayserver.domain.calendar.facade.AcademyCalendarFacade; import org.guzzing.studayserver.domain.calendar.model.Periodicity; import org.guzzing.studayserver.domain.calendar.service.AcademyCalendarService; -import org.guzzing.studayserver.testutil.WithMockCustomOAuth2LoginUser; import org.guzzing.studayserver.testutil.fixture.academycalender.AcademyCalenderFixture; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Nested; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.MethodSource; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; -import org.springframework.boot.test.mock.mockito.MockBean; import org.springframework.http.MediaType; -import org.springframework.test.web.servlet.MockMvc; -@WebMvcTest(AcademyCalendarController.class) -class AcademyCalendarControllerTest { +class AcademyCalendarControllerTest extends RestDocsSupport { - @Autowired - private MockMvc mvc; - - @Autowired - private ObjectMapper objectMapper; - - @MockBean private AcademyCalendarService academyCalendarService; - - @MockBean private AcademyCalendarFacade academyCalendarFacade; + @Override + protected Object initController() { + academyCalendarService = mock(AcademyCalendarService.class); + academyCalendarFacade = mock(AcademyCalendarFacade.class); + return new AcademyCalendarController(academyCalendarService, academyCalendarFacade); + } + @DisplayName(" 예외가 발생하는 상황을 검증한다.") @Nested class ThrowException { @DisplayName("스케줄 생성할 때 요청값에 대해 검증한다.") - @WithMockCustomOAuth2LoginUser @ParameterizedTest @MethodSource("provideInvalidCreateRequests") void createAcademyCalendar(AcademyCalendarCreateRequest academyCalendarCreateRequest) throws Exception { //Then - mvc.perform(post("/academy-schedules") + mockMvc.perform(post("/academy-schedules") .contentType(MediaType.APPLICATION_JSON) - .content(objectMapper.writeValueAsString(academyCalendarCreateRequest)) - .with(csrf())) + .content(objectMapper.writeValueAsString(academyCalendarCreateRequest))) .andExpect(status().isBadRequest()); } @DisplayName("스케줄 수정할 때 요청값에 대해 검증한다.") - @WithMockCustomOAuth2LoginUser @ParameterizedTest @MethodSource("provideInvalidUpdateRequests") void updateSchedule(AcademyCalendarUpdateRequest academyCalendarUpdateRequest) throws Exception { - mvc.perform(put("/academy-schedules") + mockMvc.perform(put("/academy-schedules") .contentType(MediaType.APPLICATION_JSON) - .content(objectMapper.writeValueAsString(academyCalendarUpdateRequest)) - .with(csrf())) + .content(objectMapper.writeValueAsString(academyCalendarUpdateRequest))) .andExpect(status().isBadRequest()); } From b0a5420b68526daf2a1f997cc876f5cebd382783 Mon Sep 17 00:00:00 2001 From: wonu606 Date: Mon, 18 Dec 2023 21:23:45 +0900 Subject: [PATCH 4/6] =?UTF-8?q?feat:=20ControllerTest=20ObjectMapper=20Loc?= =?UTF-8?q?alDate=20=EC=A7=81=EB=A0=AC=ED=99=94=20=ED=95=A0=20=EC=88=98=20?= =?UTF-8?q?=EC=9E=88=EB=8F=84=EB=A1=9D=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../studayserver/docs/RestDocsSupport.java | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/src/test/java/org/guzzing/studayserver/docs/RestDocsSupport.java b/src/test/java/org/guzzing/studayserver/docs/RestDocsSupport.java index e63a9018..3dcc48c5 100644 --- a/src/test/java/org/guzzing/studayserver/docs/RestDocsSupport.java +++ b/src/test/java/org/guzzing/studayserver/docs/RestDocsSupport.java @@ -1,16 +1,13 @@ package org.guzzing.studayserver.docs; -import static org.mockito.BDDMockito.given; import static org.springframework.restdocs.mockmvc.MockMvcRestDocumentation.documentationConfiguration; +import com.fasterxml.jackson.annotation.JsonInclude.Include; import com.fasterxml.jackson.databind.ObjectMapper; -import org.guzzing.studayserver.domain.calendar.facade.AcademyCalendarFacade; -import org.guzzing.studayserver.domain.calendar.service.AcademyCalendarService; -import org.guzzing.studayserver.domain.child.service.ChildFacade; -import org.guzzing.studayserver.domain.child.service.ChildService; +import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule; +import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.extension.ExtendWith; -import org.springframework.boot.test.mock.mockito.MockBean; import org.springframework.restdocs.RestDocumentationContextProvider; import org.springframework.restdocs.RestDocumentationExtension; import org.springframework.test.web.servlet.MockMvc; @@ -24,11 +21,17 @@ public abstract class RestDocsSupport { @BeforeEach void setup(RestDocumentationContextProvider provider) { + JavaTimeModule javaTimeModule = new JavaTimeModule(); + javaTimeModule.addSerializer(LocalDateTimeSerializer.INSTANCE); + + objectMapper = new ObjectMapper() + .setSerializationInclusion(Include.NON_NULL) + .registerModule(javaTimeModule); + mockMvc = MockMvcBuilders.standaloneSetup(initController()) + .setMessageConverters() .apply(documentationConfiguration(provider)) .build(); - - objectMapper = new ObjectMapper(); } protected abstract Object initController(); From d8185056aac71a5e1806dd9a38c0493a6f5fc98f Mon Sep 17 00:00:00 2001 From: wonu606 Date: Mon, 18 Dec 2023 21:24:41 +0900 Subject: [PATCH 5/6] =?UTF-8?q?test:=20ChildRestControllerTest=20RestDocsT?= =?UTF-8?q?est=20=EC=83=81=EC=86=8D=EB=B0=9B=EC=95=84=20=ED=85=8C=EC=8A=A4?= =?UTF-8?q?=ED=8A=B8=EB=A5=BC=20=EC=88=98=ED=96=89=ED=95=98=EB=8F=84?= =?UTF-8?q?=EB=A1=9D=20=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/ChildRestControllerTest.java | 35 +++++-------------- 1 file changed, 9 insertions(+), 26 deletions(-) diff --git a/src/test/java/org/guzzing/studayserver/domain/child/controller/ChildRestControllerTest.java b/src/test/java/org/guzzing/studayserver/domain/child/controller/ChildRestControllerTest.java index eddfc0c6..07de9e04 100644 --- a/src/test/java/org/guzzing/studayserver/domain/child/controller/ChildRestControllerTest.java +++ b/src/test/java/org/guzzing/studayserver/domain/child/controller/ChildRestControllerTest.java @@ -6,6 +6,7 @@ import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.anyLong; import static org.mockito.BDDMockito.given; +import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; import static org.springframework.http.MediaType.APPLICATION_JSON_VALUE; import static org.springframework.restdocs.operation.preprocess.Preprocessors.preprocessRequest; @@ -24,11 +25,11 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; import com.epages.restdocs.apispec.ResourceSnippetParameters; -import com.fasterxml.jackson.databind.ObjectMapper; import java.time.LocalDate; import java.time.LocalTime; import java.util.List; import java.util.stream.Stream; +import org.guzzing.studayserver.docs.RestDocsSupport; import org.guzzing.studayserver.domain.child.controller.request.ChildCreateRequest; import org.guzzing.studayserver.domain.child.controller.request.ChildModifyRequest; import org.guzzing.studayserver.domain.child.controller.response.ChildrenFindResponse; @@ -37,7 +38,6 @@ import org.guzzing.studayserver.domain.child.service.ChildWithScheduleResult; import org.guzzing.studayserver.domain.child.service.param.ChildCreateParam; import org.guzzing.studayserver.domain.child.service.result.ChildProfileImagePatchResult; -import org.guzzing.studayserver.testutil.WithMockCustomOAuth2LoginUser; import org.junit.jupiter.api.Disabled; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Nested; @@ -45,43 +45,31 @@ import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.Arguments; import org.junit.jupiter.params.provider.MethodSource; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.boot.test.autoconfigure.restdocs.AutoConfigureRestDocs; -import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; -import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; -import org.springframework.boot.test.mock.mockito.MockBean; import org.springframework.http.MediaType; import org.springframework.mock.web.MockMultipartFile; import org.springframework.restdocs.mockmvc.RestDocumentationRequestBuilders; -import org.springframework.test.web.servlet.MockMvc; import org.springframework.test.web.servlet.ResultActions; import org.springframework.web.multipart.MultipartFile; -@WebMvcTest(ChildRestController.class) -@AutoConfigureRestDocs -@AutoConfigureMockMvc(addFilters = false) -class ChildRestControllerTest { +class ChildRestControllerTest extends RestDocsSupport { private static final String TAG = "아이 API"; - @Autowired - private MockMvc mockMvc; - - @MockBean private ChildService childService; - - @MockBean private ChildFacade childFacade; - @Autowired - private ObjectMapper objectMapper; + @Override + protected Object initController() { + childService = mock(ChildService.class); + childFacade = mock(ChildFacade.class); + return new ChildRestController(childService, childFacade); + } @Nested class Create { @DisplayName("아이 생성시 정상 값이면 OK를 반환한다.") @Test - @WithMockCustomOAuth2LoginUser(memberId = 1L) void statusIsOk() throws Exception { // Given ChildCreateRequest request = new ChildCreateRequest("childName1", "초등학교 1학년"); @@ -100,7 +88,6 @@ void statusIsOk() throws Exception { @DisplayName("아이 생성시 잘못된 요청은 400에러를 반환한다.") @ParameterizedTest - @WithMockCustomOAuth2LoginUser(memberId = 1L) @MethodSource("provideInvalidRequests") void statusIsBadRequest(ChildCreateRequest invalidRequest) throws Exception { // Then @@ -126,7 +113,6 @@ private static Stream provideInvalidRequests() { } @DisplayName("멤버에 할당된 아이들의 정보를 반환한다.") - @WithMockCustomOAuth2LoginUser(memberId = 1L) @Test void findChildren_success() throws Exception { // Given @@ -149,7 +135,6 @@ void findChildren_success() throws Exception { } @DisplayName("아이를 삭제한다.") - @WithMockCustomOAuth2LoginUser() @Test void delete_success() throws Exception { // Given @@ -164,7 +149,6 @@ void delete_success() throws Exception { class modify { @DisplayName("아이의 정보를 수정한다.") - @WithMockCustomOAuth2LoginUser(memberId = 1L) @Test void success() throws Exception { // Given @@ -183,7 +167,6 @@ void success() throws Exception { } @DisplayName("잘못된 요청이 들어오면 예외를 발생시킨다.") - @WithMockCustomOAuth2LoginUser() @ParameterizedTest @MethodSource("provideInvalidRequests") void givenInvalidRequest_throwsException(ChildModifyRequest invalidRequest) throws Exception { From 7284718e7e70aac04d7572da73e2e491026f9fd2 Mon Sep 17 00:00:00 2001 From: wonu606 Date: Mon, 18 Dec 2023 21:43:49 +0900 Subject: [PATCH 6/6] =?UTF-8?q?test:=20MemberRestControllerTest=20RestDocs?= =?UTF-8?q?Support=EB=A5=BC=20=EC=83=81=EC=86=8D=EB=B0=9B=EC=95=84=20?= =?UTF-8?q?=ED=85=8C=EC=8A=A4=ED=8A=B8=EB=A5=BC=20=EC=88=98=ED=96=89?= =?UTF-8?q?=ED=95=98=EB=8F=84=EB=A1=9D=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/MemberRestControllerTest.java | 37 +++++-------------- 1 file changed, 9 insertions(+), 28 deletions(-) diff --git a/src/test/java/org/guzzing/studayserver/domain/member/controller/MemberRestControllerTest.java b/src/test/java/org/guzzing/studayserver/domain/member/controller/MemberRestControllerTest.java index 74567de2..165c0dad 100644 --- a/src/test/java/org/guzzing/studayserver/domain/member/controller/MemberRestControllerTest.java +++ b/src/test/java/org/guzzing/studayserver/domain/member/controller/MemberRestControllerTest.java @@ -2,6 +2,7 @@ import static org.mockito.ArgumentMatchers.any; import static org.mockito.BDDMockito.given; +import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.patch; @@ -11,6 +12,7 @@ import com.fasterxml.jackson.databind.ObjectMapper; import java.util.List; import java.util.stream.Stream; +import org.guzzing.studayserver.docs.RestDocsSupport; import org.guzzing.studayserver.domain.child.controller.request.ChildCreateRequest; import org.guzzing.studayserver.domain.member.controller.request.MemberRegisterRequest; import org.guzzing.studayserver.domain.member.service.MemberFacade; @@ -33,27 +35,23 @@ import org.springframework.http.MediaType; import org.springframework.test.web.servlet.MockMvc; -@WebMvcTest(MemberRestController.class) -@AutoConfigureMockMvc(addFilters = false) -class MemberRestControllerTest { +class MemberRestControllerTest extends RestDocsSupport { - @Autowired - private MockMvc mockMvc; - - @MockBean private MemberService memberService; - @MockBean private MemberFacade memberFacade; - @Autowired - private ObjectMapper objectMapper; + @Override + protected Object initController() { + memberService = mock(MemberService.class); + memberFacade = mock(MemberFacade.class); + return new MemberRestController(memberService, memberFacade); + } @Nested class Register { @DisplayName("멤버 등록시 정상 값이면 OK를 반환한다.") @Test - @WithMockCustomOAuth2LoginUser(memberId = 1L) void statusIsOk() throws Exception { // Given MemberRegisterRequest request = new MemberRegisterRequest("nickname", "email@example.com", List.of( @@ -120,23 +118,6 @@ private static Stream provideInvalidRequests() { class getInformation { @Test - @WithMockCustomOAuth2LoginUser(memberId = 999L) - @DisplayName("멤버가 존재하지 않는다면 예외를 발생시킨다.") - void whenMemberIdDoesNotExist_shouldThrowException() throws Exception { - // Given - Long nonExistentMemberId = 999L; - given(memberService.getById(nonExistentMemberId)).willThrow(new IllegalArgumentException()); - - // When & Then - mockMvc.perform(get("/members") - .contentType(MediaType.APPLICATION_JSON) - .accept(MediaType.APPLICATION_JSON) - .param("memberId", nonExistentMemberId.toString())) - .andExpect(status().isBadRequest()); - } - - @Test - @WithMockCustomOAuth2LoginUser(memberId = 1L) @DisplayName("멤버가 존재한다면 상세 정보를 반환한다.") void whenMemberIdExists_shouldReturnMemberInformation() throws Exception { // Given