-
Notifications
You must be signed in to change notification settings - Fork 4
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
[BE] 테스트 코드 리팩토링 #730
base: develop
Are you sure you want to change the base?
[BE] 테스트 코드 리팩토링 #730
Changes from all commits
bc56f07
c209ebd
1fa27ea
3a2aca5
9c296bc
4b08854
431ad83
81f6419
13bb5a6
bc6e3bf
2a90fdf
5d0cb9a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
package harustudy.backend.acceptance; | ||
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. 이 클래스에 안 쓰게 된 import문들 정리해줘야 할 것 같아요 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. 👍 반영하겠습니다!! |
||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.assertj.core.api.Assertions.assertThatCode; | ||
import static org.mockito.ArgumentMatchers.any; | ||
import static org.mockito.BDDMockito.given; | ||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; | ||
|
@@ -27,9 +28,6 @@ | |
import java.nio.charset.StandardCharsets; | ||
import java.util.List; | ||
import java.util.Map; | ||
import org.junit.jupiter.api.Assertions; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Disabled; | ||
import org.junit.jupiter.api.DisplayNameGeneration; | ||
import org.junit.jupiter.api.DisplayNameGenerator.ReplaceUnderscores; | ||
import org.junit.jupiter.api.Test; | ||
|
@@ -43,42 +41,31 @@ | |
import org.springframework.http.MediaType; | ||
import org.springframework.test.web.servlet.MockMvc; | ||
import org.springframework.test.web.servlet.MvcResult; | ||
import org.springframework.test.web.servlet.setup.MockMvcBuilders; | ||
import org.springframework.transaction.annotation.Transactional; | ||
import org.springframework.web.context.WebApplicationContext; | ||
|
||
@SuppressWarnings("NonAsciiCharacters") | ||
@DisplayNameGeneration(ReplaceUnderscores.class) | ||
@ExtendWith(MockitoExtension.class) | ||
@SpringBootTest | ||
@AutoConfigureMockMvc | ||
@Transactional | ||
@Disabled("무중단 배포를 위한 API 버저닝으로 인한 임시 disabled") | ||
class AcceptanceTest { | ||
|
||
@MockBean | ||
private OauthClients oauthClients; | ||
@Autowired | ||
private MockMvc mockMvc; | ||
|
||
@Autowired | ||
private ObjectMapper objectMapper; | ||
|
||
@Autowired | ||
private MockMvc mockMvc; | ||
@MockBean | ||
private OauthClients oauthClients; | ||
|
||
@Autowired | ||
private JwtTokenProvider jwtTokenProvider; | ||
|
||
@Autowired | ||
private TokenConfig tokenConfig; | ||
|
||
@Autowired | ||
private WebApplicationContext webApplicationContext; | ||
|
||
@BeforeEach | ||
void setUp() { | ||
this.mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build(); | ||
} | ||
|
||
@Test | ||
void 회원으로_스터디를_진행한다() throws Exception { | ||
LoginResponse 로그인_정보 = 구글_로그인을_진행한다(); | ||
|
@@ -125,32 +112,31 @@ void setUp() { | |
참여_코드로_스터디_아이디를_얻는다(로그인_정보, 참여_코드); | ||
} | ||
|
||
private List<StudyResponse> 회원으로_진행했던_모든_스터디_목록을_조회한다(LoginResponse 로그인_정보) | ||
throws Exception { | ||
long memberId = Long.parseLong(jwtTokenProvider | ||
.parseSubject(로그인_정보.tokenResponse().accessToken(), tokenConfig.secretKey())); | ||
private List<StudyResponse> 회원으로_진행했던_모든_스터디_목록을_조회한다(LoginResponse 로그인_정보) throws Exception { | ||
long memberId = Long.parseLong( | ||
jwtTokenProvider.parseSubject(로그인_정보.tokenResponse().accessToken(), tokenConfig.secretKey())); | ||
|
||
MvcResult result = mockMvc.perform( | ||
get("/api/studies") | ||
get("/api/v2/studies") | ||
.param("memberId", String.valueOf(memberId)) | ||
.header(HttpHeaders.AUTHORIZATION, 로그인_정보.createAuthorizationHeader())) | ||
.andExpect(status().isOk()) | ||
.andReturn(); | ||
|
||
String jsonResponse = result.getResponse().getContentAsString(StandardCharsets.UTF_8); | ||
StudiesResponse StudiesResponse = objectMapper.readValue(jsonResponse, | ||
StudiesResponse.class); | ||
StudiesResponse StudiesResponse = objectMapper.readValue(jsonResponse, StudiesResponse.class); | ||
|
||
return StudiesResponse.studies(); | ||
} | ||
|
||
private LoginResponse 비회원_로그인을_진행한다() throws Exception { | ||
MvcResult result = mockMvc.perform(post("/api/auth/guest")) | ||
MvcResult result = mockMvc.perform(post("/api/v2/auth/guest")) | ||
.andExpect(status().isOk()) | ||
.andReturn(); | ||
|
||
String jsonResponse = result.getResponse().getContentAsString(StandardCharsets.UTF_8); | ||
TokenResponse tokenResponse = objectMapper.readValue(jsonResponse, TokenResponse.class); | ||
|
||
return new LoginResponse(tokenResponse, null); | ||
} | ||
|
||
|
@@ -166,7 +152,7 @@ void setUp() { | |
"mock-picture")); | ||
|
||
MvcResult result = mockMvc.perform( | ||
post("/api/auth/login") | ||
post("/api/v2/auth/login") | ||
.contentType(MediaType.APPLICATION_JSON) | ||
.content(jsonRequest)) | ||
.andExpect(status().isOk()) | ||
|
@@ -175,24 +161,25 @@ void setUp() { | |
String jsonResponse = result.getResponse().getContentAsString(StandardCharsets.UTF_8); | ||
Cookie refreshToken = result.getResponse().getCookie("refreshToken"); | ||
TokenResponse tokenResponse = objectMapper.readValue(jsonResponse, TokenResponse.class); | ||
|
||
return new LoginResponse(tokenResponse, refreshToken); | ||
} | ||
|
||
private Long 스터디를_개설한다(LoginResponse 로그인_정보) throws Exception { | ||
CreateStudyRequest request = new CreateStudyRequest("studyName", 1, 20); | ||
String jsonRequest = objectMapper.writeValueAsString(request); | ||
MvcResult result = mockMvc.perform( | ||
post("/api/studies") | ||
post("/api/v2/studies") | ||
.contentType(MediaType.APPLICATION_JSON) | ||
.content(jsonRequest) | ||
.header(HttpHeaders.AUTHORIZATION, 로그인_정보.createAuthorizationHeader())) | ||
.andExpect(status().isCreated()) | ||
.andReturn(); | ||
String locationHeader = result.getResponse().getHeader(HttpHeaders.LOCATION); | ||
|
||
String locationHeader = result.getResponse().getHeader(HttpHeaders.LOCATION); | ||
String[] parsed = locationHeader.split("/"); | ||
System.out.println(locationHeader); | ||
return Long.parseLong(parsed[3]); | ||
|
||
return Long.parseLong(parsed[parsed.length - 1]); | ||
} | ||
|
||
private Long 스터디에_참여한다(LoginResponse 로그인_정보, Long 스터디_아이디) throws Exception { | ||
|
@@ -202,7 +189,7 @@ void setUp() { | |
String jsonRequest = objectMapper.writeValueAsString(request); | ||
|
||
MvcResult result = mockMvc.perform( | ||
post("/api/studies/{studyId}/participants", 스터디_아이디) | ||
post("/api/v2/studies/{studyId}/participants", 스터디_아이디) | ||
.contentType(MediaType.APPLICATION_JSON) | ||
.content(jsonRequest) | ||
.header(HttpHeaders.AUTHORIZATION, 로그인_정보.createAuthorizationHeader())) | ||
|
@@ -220,16 +207,15 @@ void setUp() { | |
WritePlanRequest request = new WritePlanRequest(참여자_아이디, Map.of("plan", "test")); | ||
String jsonRequest = objectMapper.writeValueAsString(request); | ||
|
||
mockMvc.perform(post("/api/studies/{studyId}/contents/write-plan", 스터디_아이디) | ||
mockMvc.perform(post("/api/v2/studies/{studyId}/contents/write-plan", 스터디_아이디) | ||
.contentType(MediaType.APPLICATION_JSON) | ||
.content(jsonRequest) | ||
.header(HttpHeaders.AUTHORIZATION, 로그인_정보.createAuthorizationHeader())) | ||
.andExpect(status().isOk()); | ||
} | ||
|
||
private void 스터디_상태를_다음_단계로_넘긴다(LoginResponse 로그인_정보, Long 스터디_아이디) | ||
throws Exception { | ||
mockMvc.perform(post("/api/studies/{studyId}/next-step", 스터디_아이디) | ||
private void 스터디_상태를_다음_단계로_넘긴다(LoginResponse 로그인_정보, Long 스터디_아이디) throws Exception { | ||
mockMvc.perform(post("/api/v2/studies/{studyId}/next-step", 스터디_아이디) | ||
.header(HttpHeaders.AUTHORIZATION, 로그인_정보.createAuthorizationHeader())) | ||
.andExpect(status().isNoContent()); | ||
} | ||
|
@@ -239,16 +225,15 @@ void setUp() { | |
Map.of("retrospect", "test")); | ||
String jsonRequest = objectMapper.writeValueAsString(request); | ||
|
||
mockMvc.perform(post("/api/studies/{studyId}/contents/write-retrospect", | ||
스터디_아이디) | ||
mockMvc.perform(post("/api/v2/studies/{studyId}/contents/write-retrospect", 스터디_아이디) | ||
.contentType(MediaType.APPLICATION_JSON) | ||
.content(jsonRequest) | ||
.header(HttpHeaders.AUTHORIZATION, 로그인_정보.createAuthorizationHeader())) | ||
.andExpect(status().isOk()); | ||
} | ||
|
||
private void 스터디_종료_후_결과_조회(LoginResponse 로그인_정보, Long 스터디_아이디) throws Exception { | ||
MvcResult result = mockMvc.perform(get("/api/studies/{studyId}/participants", 스터디_아이디) | ||
MvcResult result = mockMvc.perform(get("/api/v2/studies/{studyId}/participants", 스터디_아이디) | ||
.accept(MediaType.APPLICATION_JSON) | ||
.header(HttpHeaders.AUTHORIZATION, 로그인_정보.createAuthorizationHeader())) | ||
.andExpect(status().isOk()) | ||
|
@@ -268,30 +253,31 @@ void setUp() { | |
} | ||
|
||
private String 스터디_아이디로_참여_코드를_얻는다(LoginResponse 로그인_정보, Long 스터디_아이디) throws Exception { | ||
MvcResult result = mockMvc.perform(get("/api/participant-codes") | ||
MvcResult result = mockMvc.perform(get("/api/v2/participant-codes") | ||
.param("studyId", 스터디_아이디.toString()) | ||
.accept(MediaType.APPLICATION_JSON) | ||
.header(HttpHeaders.AUTHORIZATION, 로그인_정보.createAuthorizationHeader())) | ||
.andExpect(status().isOk()) | ||
.andReturn(); | ||
|
||
String response = result.getResponse().getContentAsString(StandardCharsets.UTF_8); | ||
ParticipantCodeResponse jsonResponse = objectMapper.readValue(response, | ||
ParticipantCodeResponse.class); | ||
ParticipantCodeResponse jsonResponse = objectMapper.readValue(response, ParticipantCodeResponse.class); | ||
|
||
assertThat(jsonResponse.participantCode()).hasSize(6); | ||
return jsonResponse.participantCode(); | ||
} | ||
|
||
private void 참여_코드로_스터디_아이디를_얻는다(LoginResponse 로그인_정보, String 참여_코드) throws Exception { | ||
MvcResult result = mockMvc.perform(get("/api/studies") | ||
MvcResult result = mockMvc.perform(get("/api/v2/studies") | ||
.param("participantCode", 참여_코드) | ||
.accept(MediaType.APPLICATION_JSON) | ||
.header(HttpHeaders.AUTHORIZATION, 로그인_정보.createAuthorizationHeader())) | ||
.andExpect(status().isOk()) | ||
.andReturn(); | ||
|
||
String response = result.getResponse().getContentAsString(StandardCharsets.UTF_8); | ||
Assertions.assertDoesNotThrow(() -> objectMapper.readValue(response, | ||
StudyResponse.class)); | ||
|
||
assertThatCode(() -> objectMapper.readValue(response, StudyResponse.class)) | ||
.doesNotThrowAnyException(); | ||
Comment on lines
+280
to
+281
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. 저번에 마코랑은 얘기했었던 것 같은데 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. 요 부분은 지속적으로 팀 내에서 언급이 되는 사항이라 여겨지는데 이번 PR을 기점으로 컨벤션을 잡는 것이 좋을 것 같습니다. 이번 PR이 테스트 코드 리팩토링이기도 하구요! 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. 스타일 차이이긴 한데 저는 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. 저도 둘 다 상관은 없으나 이참에 컨벤션 정하는 게 나을 것 같네요 ! 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. => 팀 회의를 통해 둘 다 사용하는 방향으로 결정했습니다. |
||
} | ||
} |
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.
이거 버저닝 걷어내고 살렸으면 작업량이 줄어드셨을텐데... 고생 많으셨어요 😭