-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
[BE/TEST] 각종 테스트
- Loading branch information
Showing
10 changed files
with
585 additions
and
5 deletions.
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
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
240 changes: 240 additions & 0 deletions
240
...ceed/src/test/java/com/gaebaljip/exceed/adapter/in/member/UpdateMemberControllerTest.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,240 @@ | ||
package com.gaebaljip.exceed.adapter.in.member; | ||
|
||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.put; | ||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; | ||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; | ||
|
||
import org.junit.jupiter.api.DisplayName; | ||
import org.junit.jupiter.api.Test; | ||
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.ResultActions; | ||
|
||
import com.gaebaljip.exceed.adapter.in.member.request.UpdateMemberRequest; | ||
import com.gaebaljip.exceed.application.port.in.member.UpdateMemberUsecase; | ||
import com.gaebaljip.exceed.common.ControllerTest; | ||
import com.gaebaljip.exceed.common.ValidationMessage; | ||
import com.gaebaljip.exceed.common.WithMockUser; | ||
|
||
@WebMvcTest(UpdateMemberController.class) | ||
public class UpdateMemberControllerTest extends ControllerTest { | ||
@MockBean private UpdateMemberUsecase updateMemberUsecase; | ||
|
||
@Test | ||
@DisplayName("회원 수정 성공") | ||
@WithMockUser | ||
void when_updateMember_expected_success() throws Exception { | ||
// given | ||
UpdateMemberRequest updateMemberRequest = | ||
UpdateMemberRequest.builder() | ||
.height(180.3) | ||
.activity("VERY_ACTIVE") | ||
.age(40) | ||
.gender("MALE") | ||
.etc("회원 수정") | ||
.build(); | ||
// when | ||
ResultActions resultActions = | ||
mockMvc.perform( | ||
put("/v1/members") | ||
.content(om.writeValueAsString(updateMemberRequest)) | ||
.contentType(MediaType.APPLICATION_JSON)); | ||
|
||
// then | ||
resultActions.andExpect(status().isOk()); | ||
} | ||
|
||
@Test | ||
@DisplayName("회원 수정 실패 - 키를 입력하지 않은 경우") | ||
@WithMockUser | ||
void when_updateMember_height_null_expected_exception() throws Exception { | ||
// given | ||
UpdateMemberRequest updateMemberRequest = | ||
UpdateMemberRequest.builder() | ||
.height(null) | ||
.activity("VERY_ACTIVE") | ||
.age(40) | ||
.gender("MALE") | ||
.etc("회원 수정") | ||
.build(); | ||
|
||
// when | ||
ResultActions resultActions = | ||
mockMvc.perform( | ||
put("/v1/members") | ||
.content(om.writeValueAsString(updateMemberRequest)) | ||
.contentType(MediaType.APPLICATION_JSON)); | ||
|
||
// then | ||
resultActions.andExpectAll( | ||
status().isBadRequest(), | ||
jsonPath("$.error.reason").value("키를 " + ValidationMessage.NOT_NULL)); | ||
} | ||
|
||
@Test | ||
@DisplayName("회원 수정 실패 - 키가 0보다 작은 경우") | ||
@WithMockUser | ||
void when_updateMember_height_negative_expected_exception() throws Exception { | ||
// given | ||
UpdateMemberRequest updateMemberRequest = | ||
UpdateMemberRequest.builder() | ||
.height(-1.0) | ||
.activity("VERY_ACTIVE") | ||
.age(40) | ||
.gender("MALE") | ||
.etc("회원 수정") | ||
.build(); | ||
|
||
// when | ||
ResultActions resultActions = | ||
mockMvc.perform( | ||
put("/v1/members") | ||
.content(om.writeValueAsString(updateMemberRequest)) | ||
.contentType(MediaType.APPLICATION_JSON)); | ||
|
||
// then | ||
resultActions.andExpectAll( | ||
status().isBadRequest(), | ||
jsonPath("$.error.reason").value("키는 " + ValidationMessage.MIN_0)); | ||
} | ||
|
||
@Test | ||
@DisplayName("회원 수정 실패 - 나이를 입력하지 않은 경우") | ||
@WithMockUser | ||
void when_updateMember_age_null_expected_exception() throws Exception { | ||
// given | ||
UpdateMemberRequest updateMemberRequest = | ||
UpdateMemberRequest.builder() | ||
.height(180.3) | ||
.activity("VERY_ACTIVE") | ||
.age(null) | ||
.gender("MALE") | ||
.etc("회원 수정") | ||
.build(); | ||
|
||
// when | ||
ResultActions resultActions = | ||
mockMvc.perform( | ||
put("/v1/members") | ||
.content(om.writeValueAsString(updateMemberRequest)) | ||
.contentType(MediaType.APPLICATION_JSON)); | ||
|
||
// then | ||
resultActions.andExpectAll( | ||
status().isBadRequest(), | ||
jsonPath("$.error.reason").value("나이를 " + ValidationMessage.NOT_NULL)); | ||
} | ||
|
||
@Test | ||
@DisplayName("회원 수정 실패 - 나이가 0보다 작은 경우") | ||
@WithMockUser | ||
void when_updateMember_age_negative_expected_exception() throws Exception { | ||
// given | ||
UpdateMemberRequest updateMemberRequest = | ||
UpdateMemberRequest.builder() | ||
.height(180.3) | ||
.activity("VERY_ACTIVE") | ||
.age(-1) | ||
.gender("MALE") | ||
.etc("회원 수정") | ||
.build(); | ||
|
||
// when | ||
ResultActions resultActions = | ||
mockMvc.perform( | ||
put("/v1/members") | ||
.content(om.writeValueAsString(updateMemberRequest)) | ||
.contentType(MediaType.APPLICATION_JSON)); | ||
|
||
// then | ||
resultActions.andExpectAll( | ||
status().isBadRequest(), | ||
jsonPath("$.error.reason").value("나이는 " + ValidationMessage.MIN_0)); | ||
} | ||
|
||
@Test | ||
@DisplayName("회원 수정 실패 - 성별이 올바르지 않은 경우") | ||
@WithMockUser | ||
void when_updateMember_gender_invalid_expected_exception() throws Exception { | ||
// given | ||
String gender = "MALLY"; | ||
|
||
UpdateMemberRequest updateMemberRequest = | ||
UpdateMemberRequest.builder() | ||
.height(180.3) | ||
.activity("VERY_ACTIVE") | ||
.age(40) | ||
.gender(gender) | ||
.etc("회원 수정") | ||
.build(); | ||
|
||
// when | ||
ResultActions resultActions = | ||
mockMvc.perform( | ||
put("/v1/members") | ||
.content(om.writeValueAsString(updateMemberRequest)) | ||
.contentType(MediaType.APPLICATION_JSON)); | ||
|
||
// then | ||
resultActions.andExpectAll( | ||
status().isBadRequest(), | ||
jsonPath("$.error.reason").value(gender + "는 올바르지 않은 값입니다.")); | ||
} | ||
|
||
@Test | ||
@DisplayName("회원 수정 실패 - 활동량이 올바르지 않은 경우") | ||
@WithMockUser | ||
void when_updateMember_activity_invalid_expected_exception() throws Exception { | ||
// given | ||
String activity = "INVALID"; | ||
|
||
UpdateMemberRequest updateMemberRequest = | ||
UpdateMemberRequest.builder() | ||
.height(180.3) | ||
.activity(activity) | ||
.age(40) | ||
.gender("MALE") | ||
.etc("회원 수정") | ||
.build(); | ||
|
||
// when | ||
ResultActions resultActions = | ||
mockMvc.perform( | ||
put("/v1/members") | ||
.content(om.writeValueAsString(updateMemberRequest)) | ||
.contentType(MediaType.APPLICATION_JSON)); | ||
|
||
// then | ||
resultActions.andExpectAll( | ||
status().isBadRequest(), | ||
jsonPath("$.error.reason").value(activity + "는 올바르지 않은 값입니다.")); | ||
} | ||
|
||
@Test | ||
@DisplayName("회원 수정 실패 - 기타사항을 입력하지 않은 경우") | ||
@WithMockUser | ||
void when_updateMember_etc_null_expected_exception() throws Exception { | ||
// given | ||
UpdateMemberRequest updateMemberRequest = | ||
UpdateMemberRequest.builder() | ||
.height(180.3) | ||
.activity("VERY_ACTIVE") | ||
.age(40) | ||
.gender("MALE") | ||
.etc(null) | ||
.build(); | ||
|
||
// when | ||
ResultActions resultActions = | ||
mockMvc.perform( | ||
put("/v1/members") | ||
.content(om.writeValueAsString(updateMemberRequest)) | ||
.contentType(MediaType.APPLICATION_JSON)); | ||
|
||
// then | ||
resultActions.andExpectAll( | ||
status().isBadRequest(), | ||
jsonPath("$.error.reason").value("기타사항을 " + ValidationMessage.NOT_BLANK)); | ||
} | ||
} |
67 changes: 67 additions & 0 deletions
67
...ceed/src/test/java/com/gaebaljip/exceed/adapter/in/member/UpdateWeightControllerTest.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,67 @@ | ||
package com.gaebaljip.exceed.adapter.in.member; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertAll; | ||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.patch; | ||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; | ||
|
||
import org.junit.jupiter.api.AfterEach; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.DisplayName; | ||
import org.junit.jupiter.api.Test; | ||
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.context.ApplicationEventPublisher; | ||
import org.springframework.http.MediaType; | ||
import org.springframework.test.context.event.ApplicationEvents; | ||
import org.springframework.test.context.event.RecordApplicationEvents; | ||
import org.springframework.test.web.servlet.ResultActions; | ||
|
||
import com.gaebaljip.exceed.adapter.in.member.request.UpdateWeightRequest; | ||
import com.gaebaljip.exceed.application.port.in.member.UpdateWeightUsecase; | ||
import com.gaebaljip.exceed.common.ControllerTest; | ||
import com.gaebaljip.exceed.common.WithMockUser; | ||
import com.gaebaljip.exceed.common.event.Events; | ||
import com.gaebaljip.exceed.common.event.UpdateWeightEvent; | ||
|
||
@RecordApplicationEvents | ||
@WebMvcTest(UpdateWeightController.class) | ||
public class UpdateWeightControllerTest extends ControllerTest { | ||
|
||
@MockBean UpdateWeightUsecase updateWeightUsecase; | ||
@Autowired ApplicationEvents events; | ||
@Autowired ApplicationEventPublisher applicationEventPublisher; | ||
|
||
@BeforeEach | ||
void setUp() { | ||
Events.setPublisher(applicationEventPublisher); | ||
} | ||
|
||
@AfterEach | ||
void tearDown() { | ||
Events.reset(); | ||
} | ||
|
||
@Test | ||
@WithMockUser(memberId = 1L) | ||
@DisplayName("몸무게 수정 성공") | ||
void when_updateWeight_expected_success() throws Exception { | ||
// given | ||
UpdateWeightRequest updateWeightRequest = | ||
UpdateWeightRequest.builder().weight(50.0).targetWeight(70.5).build(); | ||
|
||
// when | ||
ResultActions resultActions = | ||
mockMvc.perform( | ||
patch("/v1/members/weight") | ||
.content(om.writeValueAsString(updateWeightRequest)) | ||
.contentType(MediaType.APPLICATION_JSON)); | ||
|
||
long count = events.stream(UpdateWeightEvent.class).count(); | ||
|
||
// then | ||
resultActions.andExpectAll(status().isOk()); | ||
assertAll(() -> assertEquals(1, count)); | ||
} | ||
} |
Oops, something went wrong.