-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: 회원 이름 정보 수정 Controller, Service 테스트코드 작성 (#94)
- Loading branch information
Showing
10 changed files
with
190 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
62 changes: 62 additions & 0 deletions
62
module-domain/src/test/java/com/depromeet/service/MemberServiceTest.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,62 @@ | ||
package com.depromeet.service; | ||
|
||
import com.depromeet.member.domain.Member; | ||
import com.depromeet.member.domain.MemberRole; | ||
import com.depromeet.member.port.out.persistence.MemberPersistencePort; | ||
import com.depromeet.member.service.MemberService; | ||
import com.depromeet.mock.FakeMemberRepository; | ||
import org.assertj.core.api.Assertions; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
|
||
class MemberServiceTest { | ||
private MemberPersistencePort fakeMemberRepository; | ||
|
||
private MemberService memberService; | ||
|
||
private Long userId = 1L; | ||
private Member member; | ||
|
||
@BeforeEach | ||
void init() { | ||
fakeMemberRepository = new FakeMemberRepository(); | ||
|
||
// Member create | ||
member = | ||
Member.builder() | ||
.id(userId) | ||
.name("member1") | ||
.email("[email protected]") | ||
.role(MemberRole.USER) | ||
.build(); | ||
fakeMemberRepository.save(member); | ||
|
||
memberService = new MemberService(fakeMemberRepository); | ||
} | ||
|
||
@Test | ||
void 회원의_이름을_수정할_수_있다() { | ||
// given | ||
Long memberId = 1L; | ||
String newName = "테스트"; | ||
|
||
// when | ||
Member member1 = memberService.updateName(memberId, newName); | ||
|
||
// then | ||
Assertions.assertThat(member1.getName()).isEqualTo(newName); | ||
} | ||
|
||
@Test | ||
void 회원의_이름은_공백을_허용하지_않는다() { | ||
// given | ||
Long memberId = 1L; | ||
String newName = ""; | ||
|
||
// when | ||
|
||
// then | ||
Assertions.assertThatThrownBy(() -> memberService.updateName(memberId, newName)) | ||
.hasMessage("멤버의 이름은 공백이 허용되지 않습니다"); | ||
} | ||
} |
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
2 changes: 1 addition & 1 deletion
2
...t/memory/config/ControllerTestConfig.java → ...epromeet/config/ControllerTestConfig.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
102 changes: 102 additions & 0 deletions
102
module-presentation/src/test/java/com/depromeet/member/controller/MemberControllerTest.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,102 @@ | ||
package com.depromeet.member.controller; | ||
|
||
import static org.mockito.ArgumentMatchers.anyLong; | ||
import static org.mockito.ArgumentMatchers.anyString; | ||
import static org.mockito.Mockito.when; | ||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.patch; | ||
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print; | ||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; | ||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; | ||
|
||
import com.depromeet.config.ControllerTestConfig; | ||
import com.depromeet.member.api.MemberController; | ||
import com.depromeet.member.domain.Member; | ||
import com.depromeet.member.domain.MemberRole; | ||
import com.depromeet.member.facade.MemberFacade; | ||
import com.depromeet.memory.mock.WithCustomMockMember; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
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.http.MediaType; | ||
|
||
@WebMvcTest(MemberController.class) | ||
public class MemberControllerTest extends ControllerTestConfig { | ||
@Autowired private ObjectMapper objectMapper; | ||
|
||
@MockBean MemberFacade memberFacade; | ||
|
||
@Test | ||
@WithCustomMockMember | ||
void 회원의_이름을_수정합니다() throws Exception { | ||
Map<String, String> requestBody = new HashMap<>(); | ||
requestBody.put("name", "테스트"); | ||
|
||
Member member = new Member(1L, 3000, "테스트", "[email protected]", MemberRole.USER, "aa"); | ||
when(memberFacade.updateName(anyLong(), anyString())).thenReturn(member); | ||
|
||
mockMvc.perform( | ||
patch("/member") | ||
.contentType(MediaType.APPLICATION_JSON) | ||
.content(objectMapper.writeValueAsString(requestBody))) | ||
.andExpect(status().isOk()) | ||
.andExpect(jsonPath("$.code").value("MEMBER_4")) | ||
.andExpect(jsonPath("$.message").value("멤버 이름 수정에 성공하였습니다")) | ||
.andExpect(jsonPath("$.data.name").value("테스트")) | ||
.andDo(print()); | ||
} | ||
|
||
@Test | ||
@WithCustomMockMember | ||
void 회원의_이름은_Null을_허용하지_않습니다() throws Exception { | ||
Map<String, String> requestBody = new HashMap<>(); | ||
requestBody.put("name", null); | ||
|
||
mockMvc.perform( | ||
patch("/member") | ||
.contentType(MediaType.APPLICATION_JSON) | ||
.content(objectMapper.writeValueAsString(requestBody))) | ||
.andExpect(status().isBadRequest()) | ||
.andExpect(jsonPath("$.code").value("COMMON_1")) | ||
.andExpect(jsonPath("$.message").value("입력 값 검증에 실패하였습니다")) | ||
.andExpect(jsonPath("$.data.fieldErrors[0].reason").value("must not be blank")) | ||
.andDo(print()); | ||
} | ||
|
||
@Test | ||
@WithCustomMockMember | ||
void 회원의_이름은_공백을_허용하지_않습니다() throws Exception { | ||
Map<String, String> requestBody = new HashMap<>(); | ||
requestBody.put("name", ""); | ||
|
||
mockMvc.perform( | ||
patch("/member") | ||
.contentType(MediaType.APPLICATION_JSON) | ||
.content(objectMapper.writeValueAsString(requestBody))) | ||
.andExpect(status().isBadRequest()) | ||
.andExpect(jsonPath("$.code").value("COMMON_1")) | ||
.andExpect(jsonPath("$.message").value("입력 값 검증에 실패하였습니다")) | ||
.andExpect(jsonPath("$.data.fieldErrors[0].reason").value("must not be blank")) | ||
.andDo(print()); | ||
} | ||
|
||
@Test | ||
@WithCustomMockMember | ||
void 회원의_이름은_빈문자열을_허용하지_않습니다() throws Exception { | ||
Map<String, String> requestBody = new HashMap<>(); | ||
requestBody.put("name", " "); | ||
|
||
mockMvc.perform( | ||
patch("/member") | ||
.contentType(MediaType.APPLICATION_JSON) | ||
.content(objectMapper.writeValueAsString(requestBody))) | ||
.andExpect(status().isBadRequest()) | ||
.andExpect(jsonPath("$.code").value("COMMON_1")) | ||
.andExpect(jsonPath("$.message").value("입력 값 검증에 실패하였습니다")) | ||
.andExpect(jsonPath("$.data.fieldErrors[0].reason").value("must not be blank")) | ||
.andDo(print()); | ||
} | ||
} |
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