-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
ControllerTest RestDocsSupport를 상속받아 테스트를 수행하도록 리팩토링 #132
Open
wonu606
wants to merge
6
commits into
develop
Choose a base branch
from
test-controller
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
8baad04
test: ControllerTest에서 사용할 RestDocs Abstract Class 구현
wonu606 998be36
refactor: AcademyCalendarController RequestDTO 레코드로 변경
wonu606 f2c1bd3
test: AcademyCalendarControllerTest RestDocsSupport 상속받아 테스트 수행하도록 수정
wonu606 b0a5420
feat: ControllerTest ObjectMapper LocalDate 직렬화 할 수 있도록 추가
wonu606 d818505
test: ChildRestControllerTest RestDocsTest 상속받아 테스트를 수행하도록 변경
wonu606 7284718
test: MemberRestControllerTest RestDocsSupport를 상속받아 테스트를 수행하도록 수정
wonu606 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 4 additions & 12 deletions
16
.../java/org/guzzing/studayserver/domain/calendar/controller/dto/request/AttendanceDate.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) { | ||
} |
16 changes: 4 additions & 12 deletions
16
...main/java/org/guzzing/studayserver/domain/calendar/controller/dto/request/LessonTime.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) { | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 38 additions & 0 deletions
38
src/test/java/org/guzzing/studayserver/docs/RestDocsSupport.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package org.guzzing.studayserver.docs; | ||
|
||
import static org.springframework.restdocs.mockmvc.MockMvcRestDocumentation.documentationConfiguration; | ||
|
||
import com.fasterxml.jackson.annotation.JsonInclude.Include; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
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.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) { | ||
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(); | ||
} | ||
|
||
protected abstract Object initController(); | ||
} |
40 changes: 14 additions & 26 deletions
40
...va/org/guzzing/studayserver/domain/calendar/controller/AcademyCalendarControllerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,74 +1,62 @@ | ||
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; | ||
import org.guzzing.studayserver.domain.calendar.controller.dto.request.LessonTime; | ||
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); | ||
Comment on lines
+33
to
+34
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 저희 Mock 제거하기로 해서, 이 코드는 없어질 것 같네요! |
||
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()); | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
설정 클래스가 아닌 추상 클래스를 선택하신 이유가 궁금합니다!!