From f95e1627dd91dffff786e72ccb67414c73cc5fb7 Mon Sep 17 00:00:00 2001 From: westzeroright Date: Fri, 15 Nov 2024 02:08:50 +0900 Subject: [PATCH] =?UTF-8?q?test:=20=EB=A9=A4=EB=B2=84=20=EC=BB=A8=ED=8A=B8?= =?UTF-8?q?=EB=A1=A4=EB=9F=AC=20=ED=85=8C=EC=8A=A4=ED=8A=B8=20=EC=BD=94?= =?UTF-8?q?=EB=93=9C=20=EC=9E=91=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 회원 조회 - 회원 수정 - 회원 삭제 --- .../presentation/MemberControllerTest.java | 95 +++++++++++++++++++ 1 file changed, 95 insertions(+) create mode 100644 src/test/java/com/ordertogether/team14_be/member/presentation/MemberControllerTest.java diff --git a/src/test/java/com/ordertogether/team14_be/member/presentation/MemberControllerTest.java b/src/test/java/com/ordertogether/team14_be/member/presentation/MemberControllerTest.java new file mode 100644 index 0000000..3a31eb5 --- /dev/null +++ b/src/test/java/com/ordertogether/team14_be/member/presentation/MemberControllerTest.java @@ -0,0 +1,95 @@ +package com.ordertogether.team14_be.member.presentation; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.mockito.ArgumentMatchers.anyLong; +import static org.mockito.ArgumentMatchers.eq; +import static org.mockito.Mockito.*; + +import com.ordertogether.team14_be.auth.persistence.JwtUtil; +import com.ordertogether.team14_be.common.web.response.ApiResponse; +import com.ordertogether.team14_be.member.application.dto.MemberInfoRequest; +import com.ordertogether.team14_be.member.application.dto.MemberInfoResponse; +import com.ordertogether.team14_be.member.application.service.MemberService; +import com.ordertogether.team14_be.member.persistence.entity.Member; +import io.jsonwebtoken.Claims; +import jakarta.servlet.http.HttpServletResponse; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; +import org.springframework.http.HttpHeaders; +import org.springframework.http.ResponseEntity; + +class MemberControllerTest { + + @Mock private MemberService memberService; + + @Mock private JwtUtil jwtUtil; + + @InjectMocks private MemberController memberController; + + @Mock private Member member; + private MemberInfoResponse memberInfoResponse; + + @BeforeEach + void setUp() { + MockitoAnnotations.openMocks(this); + when(member.getId()).thenReturn(1L); + memberInfoResponse = new MemberInfoResponse("deliveryName", "010-1234-5678", 0); + } + + @Test + void getMemberInfo_Success() { + when(memberService.findMemberInfo(member.getId())).thenReturn(memberInfoResponse); + + ResponseEntity> response = + memberController.getMemberInfo(member); + + assertEquals(200, response.getStatusCodeValue()); + assertEquals("회원 정보를 가져옵니다.", response.getBody().getMessage()); + assertEquals(memberInfoResponse, response.getBody().getData()); + verify(memberService, times(1)).findMemberInfo(member.getId()); + } + + @Test + void modifyMemberInfo_Success() { + MemberInfoRequest memberInfoRequest = new MemberInfoRequest("deliveryName", "010-5678-1234"); + + MemberInfoResponse updatedResponse = new MemberInfoResponse("deliveryName", "010-5678-1234", 0); + + when(memberService.modifyMember(anyLong(), eq("deliveryName"), eq("010-5678-1234"))) + .thenReturn(updatedResponse); + + ResponseEntity> response = + memberController.modifyMemberInfo(member, memberInfoRequest); + + assertEquals(200, response.getStatusCodeValue()); + assertEquals("회원 정보가 수정되었습니다.", response.getBody().getMessage()); + assertEquals(updatedResponse, response.getBody().getData()); + verify(memberService, times(1)) + .modifyMember(anyLong(), eq("deliveryName"), eq("010-5678-1234")); + } + + @Test + void deleteMember_Success() { + String authorizationHeader = "Bearer testToken"; + HttpServletResponse httpServletResponse = mock(HttpServletResponse.class); + + Claims claims = mock(Claims.class); + when(claims.getSubject()).thenReturn("1"); + when(jwtUtil.decodeJwt("testToken")).thenReturn(claims); + + doNothing().when(memberService).deleteMember(anyLong()); + + ResponseEntity response = + memberController.deleteMember(authorizationHeader, httpServletResponse); + + assertEquals(200, response.getStatusCodeValue()); + assertEquals("회원탈퇴 성공", response.getBody().getMessage()); + verify(jwtUtil, times(1)).decodeJwt("testToken"); + verify(memberService, times(1)).deleteMember(anyLong()); + assertEquals("", response.getBody().getData()); + assertEquals(1, response.getHeaders().get(HttpHeaders.SET_COOKIE).size()); + } +}