Skip to content

Commit

Permalink
refactor: test 코드 수정
Browse files Browse the repository at this point in the history
  • Loading branch information
soeunnPark committed Nov 22, 2024
1 parent 0baba52 commit 08c692c
Showing 1 changed file with 111 additions and 108 deletions.
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package org.badminton.api.application.member;

import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.Mockito.*;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

import org.badminton.api.interfaces.match.dto.MatchResultResponse;
import org.badminton.domain.common.enums.MatchStatus;
import org.badminton.domain.common.enums.MatchType;
Expand Down Expand Up @@ -34,141 +36,142 @@

class MemberFacadeTest {

@Mock
private MemberService memberService;

@Mock
private LeagueRecordService leagueRecordService;
@Mock
private MemberService memberService;

@Mock
private ClubMemberService clubMemberService;
@Mock
private LeagueRecordService leagueRecordService;

@Mock
private MatchResultService matchResultService;
@Mock
private ClubMemberService clubMemberService;

@InjectMocks
private MemberFacade memberFacade;
@Mock
private MatchResultService matchResultService;

@BeforeEach
void setUp() {
MockitoAnnotations.openMocks(this);
}
@InjectMocks
private MemberFacade memberFacade;

@Test
@DisplayName("회원 마이페이지 조회 API 테스트")
void getMemberMyPageInfoTest() {
String memberToken = "testToken";
@BeforeEach
void setUp() {
MockitoAnnotations.openMocks(this);
}

List<ClubMemberMyPageInfo> clubMemberMyPageInfos = new ArrayList<>();
when(clubMemberService.getClubMembers(memberToken)).thenReturn(clubMemberMyPageInfos);
@Test
@DisplayName("회원 마이페이지 조회 API 테스트")
void getMemberMyPageInfoTest() {
String memberToken = "testToken";

LeagueRecord leagueRecord = mock(LeagueRecord.class);
when(leagueRecordService.getLeagueRecord(memberToken)).thenReturn(leagueRecord);
List<ClubMemberMyPageInfo> clubMemberMyPageInfos = new ArrayList<>();
when(clubMemberService.getClubMembers(memberToken)).thenReturn(clubMemberMyPageInfos);

LeagueRecordInfo leagueRecordInfo = LeagueRecordInfo.from(leagueRecord);
MemberMyPageInfo expectedInfo = new MemberMyPageInfo("testToken", "김철수", "abcd@naver.com", "www.naver.com",
Member.MemberTier.BRONZE, 3, 2, 1, 6, null);
when(memberService.getMemberInfo(memberToken, leagueRecordInfo, clubMemberMyPageInfos)).thenReturn(
expectedInfo);
LeagueRecord leagueRecord = mock(LeagueRecord.class);
when(leagueRecordService.getLeagueRecord(memberToken)).thenReturn(leagueRecord);

MemberMyPageInfo result = memberFacade.getMemberMyPageInfo(memberToken);
LeagueRecordInfo leagueRecordInfo = LeagueRecordInfo.from(leagueRecord);
MemberMyPageInfo expectedInfo = new MemberMyPageInfo("testToken", "김철수", "abcd@naver.com", "www.naver.com",
Member.MemberTier.BRONZE, 3, 2, 1, 6, null);
when(memberService.getMemberInfo(memberToken, leagueRecordInfo, clubMemberMyPageInfos)).thenReturn(
expectedInfo);

assertNotNull(result);
assertEquals(expectedInfo, result);
MemberMyPageInfo result = memberFacade.getMemberMyPageInfo(memberToken);

verify(clubMemberService).getClubMembers(memberToken);
verify(leagueRecordService).getLeagueRecord(memberToken);
verify(memberService).getMemberInfo(memberToken, leagueRecordInfo, clubMemberMyPageInfos);
}
assertNotNull(result);
assertEquals(expectedInfo, result);

@Test
@DisplayName("회원 정보 간단 조회 API 테스트")
void getSimpleMemberTest() {
String memberToken = "testToken";
SimpleMemberInfo expectedInfo = new SimpleMemberInfo(null, "김철수", "abcd@naver.com", Member.MemberTier.BRONZE,
"www.naver.com");
verify(clubMemberService).getClubMembers(memberToken);
verify(leagueRecordService).getLeagueRecord(memberToken);
verify(memberService).getMemberInfo(memberToken, leagueRecordInfo, clubMemberMyPageInfos);
}

when(memberService.getSimpleMember(memberToken)).thenReturn(expectedInfo);
@Test
@DisplayName("회원 정보 간단 조회 API 테스트")
void getSimpleMemberTest() {
String memberToken = "testToken";
SimpleMemberInfo expectedInfo = new SimpleMemberInfo(null, "member_token_1", "김철수", "abcd@naver.com",
Member.MemberTier.BRONZE,
"www.naver.com");

SimpleMemberInfo result = memberFacade.getSimpleMember(memberToken);
when(memberService.getSimpleMember(memberToken)).thenReturn(expectedInfo);

assertNotNull(result);
assertEquals(expectedInfo, result);
SimpleMemberInfo result = memberFacade.getSimpleMember(memberToken);

verify(memberService).getSimpleMember(memberToken);
}
assertNotNull(result);
assertEquals(expectedInfo, result);

@Test
@DisplayName("회원 정보 업데이트 API 테스트")
void updateProfileTest() {
String memberToken = "testToken";
String imageUrl = "image_url";
String name = "name";
MemberUpdateInfo expectedInfo = new MemberUpdateInfo("testToken", null, "김철수", "abc@naver.com", "providerId",
"profileImage");
verify(memberService).getSimpleMember(memberToken);
}

when(memberService.updateProfile(memberToken, imageUrl, name)).thenReturn(expectedInfo);
@Test
@DisplayName("회원 정보 업데이트 API 테스트")
void updateProfileTest() {
String memberToken = "testToken";
String imageUrl = "image_url";
String name = "name";
MemberUpdateInfo expectedInfo = new MemberUpdateInfo("testToken", null, "김철수", "abc@naver.com", "providerId",
"profileImage");

MemberUpdateInfo result = memberFacade.updateProfile(memberToken, imageUrl, name);
when(memberService.updateProfile(memberToken, imageUrl, name)).thenReturn(expectedInfo);

assertNotNull(result);
assertEquals(expectedInfo, result);
MemberUpdateInfo result = memberFacade.updateProfile(memberToken, imageUrl, name);

verify(memberService).updateProfile(memberToken, imageUrl, name);
}
assertNotNull(result);
assertEquals(expectedInfo, result);

@Test
@DisplayName("회원이 가입되어 있는 동호회 조회 API 테스트")
void getMyClubsTest() {
String memberToken = "testToken";
List<ClubCardInfo> expectedClubs = new ArrayList<>();
verify(memberService).updateProfile(memberToken, imageUrl, name);
}

when(clubMemberService.getClubsByMemberToken(memberToken)).thenReturn(expectedClubs);
@Test
@DisplayName("회원이 가입되어 있는 동호회 조회 API 테스트")
void getMyClubsTest() {
String memberToken = "testToken";
List<ClubCardInfo> expectedClubs = new ArrayList<>();

List<ClubCardInfo> result = memberFacade.getMyClubs(memberToken);
when(clubMemberService.getClubsByMemberToken(memberToken)).thenReturn(expectedClubs);

assertNotNull(result);
assertEquals(expectedClubs, result);
List<ClubCardInfo> result = memberFacade.getMyClubs(memberToken);

verify(clubMemberService).getClubsByMemberToken(memberToken);
}
assertNotNull(result);
assertEquals(expectedClubs, result);

@Test
@DisplayName("회원 매치 전적 조회 API 테스트")
void getMemberMatchResultsTest() {
String memberToken = "testToken";
int page = 0;
int size = 10;
verify(clubMemberService).getClubsByMemberToken(memberToken);
}

// Mock ClubMemberMyPageInfo
ClubMemberMyPageInfo clubMember = mock(ClubMemberMyPageInfo.class);
when(clubMember.clubMemberId()).thenReturn(1L); // 반환 타입은 Long
when(clubMemberService.getClubMembers(memberToken)).thenReturn(
List.of(clubMember)); // 반환 타입은 List<ClubMemberMyPageInfo>
@Test
@DisplayName("회원 매치 전적 조회 API 테스트")
void getMemberMatchResultsTest() {
String memberToken = "testToken";
int page = 0;
int size = 10;

// Mock MatchResultInfo
MatchResultInfo matchResult = mock(MatchResultInfo.class);
when(matchResult.matchId()).thenReturn(1L);
when(matchResult.leagueId()).thenReturn(10L);
when(matchResult.matchType()).thenReturn(MatchType.SINGLES);
when(matchResult.singlesMatch()).thenReturn(mock(SinglesMatchResultInfo.class));
when(matchResult.matchStatus()).thenReturn(MatchStatus.FINISHED);
when(matchResult.leagueAt()).thenReturn(LocalDateTime.now());

// MatchResultService의 동작 설정
when(matchResultService.getAllMatchResultsByClubMember(1L)).thenReturn(List.of(matchResult));
// Mock ClubMemberMyPageInfo
ClubMemberMyPageInfo clubMember = mock(ClubMemberMyPageInfo.class);
when(clubMember.clubMemberId()).thenReturn(1L); // 반환 타입은 Long
when(clubMemberService.getClubMembers(memberToken)).thenReturn(
List.of(clubMember)); // 반환 타입은 List<ClubMemberMyPageInfo>

// Mock MatchResultInfo
MatchResultInfo matchResult = mock(MatchResultInfo.class);
when(matchResult.matchId()).thenReturn(1L);
when(matchResult.leagueId()).thenReturn(10L);
when(matchResult.matchType()).thenReturn(MatchType.SINGLES);
when(matchResult.singlesMatch()).thenReturn(mock(SinglesMatchResultInfo.class));
when(matchResult.matchStatus()).thenReturn(MatchStatus.FINISHED);
when(matchResult.leagueAt()).thenReturn(LocalDateTime.now());

// MatchResultService의 동작 설정
when(matchResultService.getAllMatchResultsByClubMember(1L)).thenReturn(List.of(matchResult));

// Perform the test
Page<MatchResultResponse> result = memberFacade.getMemberMatchResults(memberToken, page, size);
// Perform the test
Page<MatchResultResponse> result = memberFacade.getMemberMatchResults(memberToken, page, size);

// Assertions
assertNotNull(result);
assertEquals(1, result.getContent().size());
assertEquals(matchResult.matchId(), result.getContent().get(0).matchId());

// Verify 호출 확인
verify(clubMemberService).getClubMembers(memberToken);
verify(matchResultService).getAllMatchResultsByClubMember(1L);
}
// Assertions
assertNotNull(result);
assertEquals(1, result.getContent().size());
assertEquals(matchResult.matchId(), result.getContent().get(0).matchId());

// Verify 호출 확인
verify(clubMemberService).getClubMembers(memberToken);
verify(matchResultService).getAllMatchResultsByClubMember(1L);
}

}

0 comments on commit 08c692c

Please sign in to comment.