-
Notifications
You must be signed in to change notification settings - Fork 0
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
[OMCT-369] 피드 목록 조회 테스트 추가 #194
Merged
Merged
Changes from 3 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
2e2082e
fixture: Member 엔티티를 만들기 위한 fixture 추가
Curry4182 aa89187
fixture: feed 목록 테스트를 위한 fixture 추가
Curry4182 d12265c
test: 피드 목록 조회 테스트
Curry4182 b7874e8
style: 파일 마지막에 개행 추가
Curry4182 c41e9c6
test: 입력값이 정확하게 들어올 수 있는지 확인 되도록 수정
Curry4182 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
121 changes: 121 additions & 0 deletions
121
...est/java/com/programmers/bucketback/domains/feed/implementation/FeedCursorReaderTest.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,121 @@ | ||
package com.programmers.bucketback.domains.feed.implementation; | ||
|
||
import static org.assertj.core.api.Assertions.*; | ||
import static org.mockito.BDDMockito.*; | ||
|
||
import java.util.List; | ||
import java.util.stream.Stream; | ||
|
||
import org.junit.jupiter.api.DisplayName; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
import org.junit.jupiter.params.ParameterizedTest; | ||
import org.junit.jupiter.params.provider.Arguments; | ||
import org.junit.jupiter.params.provider.MethodSource; | ||
import org.mockito.InjectMocks; | ||
import org.mockito.Mock; | ||
import org.mockito.junit.jupiter.MockitoExtension; | ||
|
||
import com.programmers.bucketback.common.cursor.CursorPageParameters; | ||
import com.programmers.bucketback.common.cursor.CursorPageParametersBuilder; | ||
import com.programmers.bucketback.common.cursor.CursorSummary; | ||
import com.programmers.bucketback.common.cursor.CursorUtils; | ||
import com.programmers.bucketback.common.model.Hobby; | ||
import com.programmers.bucketback.domains.feed.FeedBuilder; | ||
import com.programmers.bucketback.domains.feed.domain.Feed; | ||
import com.programmers.bucketback.domains.feed.model.FeedCursorSummary; | ||
import com.programmers.bucketback.domains.feed.model.FeedCursorSummaryBuilder; | ||
import com.programmers.bucketback.domains.feed.model.FeedCursorSummaryLike; | ||
import com.programmers.bucketback.domains.feed.model.FeedCursorSummaryLikeBuilder; | ||
import com.programmers.bucketback.domains.feed.model.FeedSortCondition; | ||
import com.programmers.bucketback.domains.feed.repository.FeedLikeRepository; | ||
import com.programmers.bucketback.domains.feed.repository.FeedRepository; | ||
import com.programmers.bucketback.domains.member.domain.Member; | ||
import com.programmers.bucketback.domains.member.domain.MemberBuilder; | ||
import com.programmers.bucketback.domains.member.implementation.MemberReader; | ||
|
||
@ExtendWith(MockitoExtension.class) | ||
class FeedCursorReaderTest { | ||
|
||
private static final int DEFAULT_PAGE_SIZE = 20; | ||
|
||
@InjectMocks | ||
private FeedCursorReader feedCursorReader; | ||
|
||
@Mock | ||
private FeedRepository feedRepository; | ||
|
||
@Mock | ||
private FeedLikeRepository feedLikeRepository; | ||
|
||
@Mock | ||
private MemberReader memberReader; | ||
|
||
@Mock | ||
private FeedReader feedReader; | ||
|
||
static Stream<Arguments> provideParameters() { | ||
return Stream.of( | ||
Arguments.of(CursorPageParametersBuilder.build()), | ||
Arguments.of(CursorPageParametersBuilder.buildWithNull()) | ||
); | ||
} | ||
|
||
@ParameterizedTest | ||
@DisplayName("피드 목록 조회 테스트를 한다.") | ||
@MethodSource(value = "provideParameters") | ||
void readFeedListTest(final CursorPageParameters parameters) { | ||
// given | ||
List<FeedCursorSummary> feedCursorSummaries = FeedCursorSummaryBuilder.buildMany(); | ||
List<FeedCursorSummaryLike> feedCursorSummaryLikes = FeedCursorSummaryLikeBuilder | ||
.buildMany(feedCursorSummaries, | ||
true | ||
); | ||
|
||
CursorSummary<FeedCursorSummaryLike> expectedSummaries = CursorUtils.getCursorSummaries(feedCursorSummaryLikes); | ||
|
||
Hobby hobby = Hobby.BASEBALL; | ||
String nickname = " nickname "; | ||
Long memberId = 1L; | ||
Long myPageMemberId = 1L; | ||
FeedSortCondition sortCondition = FeedSortCondition.RECENT; | ||
Member member = MemberBuilder.build(memberId); | ||
int pageSize = parameters.size() == null ? DEFAULT_PAGE_SIZE : parameters.size(); | ||
|
||
given(memberReader.readByNickname(nickname.trim())).willReturn(member); | ||
|
||
given(feedRepository.findAllByCursor( | ||
eq(myPageMemberId), | ||
anyBoolean(), | ||
eq(hobby), | ||
eq(sortCondition), | ||
eq(parameters.cursorId()), | ||
eq(pageSize) | ||
) | ||
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. 모든 내용을 eq를 사용해서 해결하신 이유가 있나요? 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. anyBoolean()을 사용려다보니 matcher로 통일해야 해서 그렇게 했습니다. |
||
).willReturn(feedCursorSummaries); | ||
|
||
given(feedLikeRepository.existsByMemberIdAndFeed( | ||
eq(memberId), | ||
any(Feed.class) | ||
) | ||
).willReturn(true); | ||
|
||
given(feedReader.read(anyLong())).willReturn(FeedBuilder.build()); | ||
|
||
// when | ||
CursorSummary<FeedCursorSummaryLike> actualSummaries = feedCursorReader.getFeedByCursor( | ||
hobby, | ||
nickname, | ||
true, | ||
memberId, | ||
sortCondition, | ||
parameters | ||
); | ||
|
||
// then | ||
assertThat(actualSummaries) | ||
.usingRecursiveComparison() | ||
.isEqualTo(expectedSummaries); | ||
|
||
} | ||
|
||
} |
25 changes: 25 additions & 0 deletions
25
...Fixtures/java/com/programmers/bucketback/domains/feed/model/FeedCursorSummaryBuilder.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,25 @@ | ||
package com.programmers.bucketback.domains.feed.model; | ||
|
||
import java.util.List; | ||
import java.util.stream.LongStream; | ||
|
||
import lombok.AccessLevel; | ||
import lombok.NoArgsConstructor; | ||
|
||
@NoArgsConstructor(access = AccessLevel.PRIVATE) | ||
public class FeedCursorSummaryBuilder { | ||
|
||
public static FeedCursorSummary build(final Long feedId) { | ||
return FeedCursorSummary.builder() | ||
.cursorId("202301010000000000000" + feedId) | ||
.feedId(feedId) | ||
.content("content" + feedId) | ||
.build(); | ||
} | ||
|
||
public static List<FeedCursorSummary> buildMany() { | ||
return LongStream.range(1, 11) | ||
.mapToObj(FeedCursorSummaryBuilder::build) | ||
.toList(); | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
...ures/java/com/programmers/bucketback/domains/feed/model/FeedCursorSummaryLikeBuilder.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,26 @@ | ||
package com.programmers.bucketback.domains.feed.model; | ||
|
||
import java.util.List; | ||
|
||
import lombok.AccessLevel; | ||
import lombok.NoArgsConstructor; | ||
|
||
@NoArgsConstructor(access = AccessLevel.PRIVATE) | ||
public class FeedCursorSummaryLikeBuilder { | ||
|
||
public static FeedCursorSummaryLike build( | ||
final FeedCursorSummary feedCursorSummary, | ||
final boolean isLike | ||
) { | ||
return feedCursorSummary.of(isLike); | ||
} | ||
|
||
public static List<FeedCursorSummaryLike> buildMany( | ||
final List<FeedCursorSummary> feedCursorSummaries, | ||
final boolean isLike | ||
) { | ||
return feedCursorSummaries.stream() | ||
.map(feedCursorSummary -> build(feedCursorSummary, isLike)) | ||
.toList(); | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
...src/testFixtures/java/com/programmers/bucketback/domains/member/domain/MemberBuilder.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,36 @@ | ||
package com.programmers.bucketback.domains.member.domain; | ||
|
||
import org.springframework.test.util.ReflectionTestUtils; | ||
|
||
import com.programmers.bucketback.domains.member.domain.vo.Role; | ||
|
||
import lombok.AccessLevel; | ||
import lombok.NoArgsConstructor; | ||
|
||
@NoArgsConstructor(access = AccessLevel.PRIVATE) | ||
public class MemberBuilder { | ||
|
||
public static Member build(final Long memberId) { | ||
Member member = Member.builder() | ||
.nickname("nickname") | ||
.email("[email protected]") | ||
.password("password") | ||
.role(Role.USER) | ||
.build(); | ||
|
||
setMemberId(member, memberId); | ||
|
||
return member; | ||
} | ||
|
||
private static void setMemberId( | ||
final Member member, | ||
final Long id | ||
) { | ||
ReflectionTestUtils.setField( | ||
member, | ||
"id", | ||
id | ||
); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
내부 로직에 있을텐데 이렇게 해주신 이유가 있나요?
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.
내부 로직과 별개로 내부 동작이 옳바르게 작동되는지 확인하기 위해 테스트용 데이터를 만들었습니다.
이 과정에서 내부와 로직이 같을 수 있다고 생각합니다
예로들어 pageSize는 테스트에서 CursorPageParameters 객체의 size 값이 null 일 때 DEFAULT_PAGE_SIZE로 반환되는지 확인하기 위한 데이터를 직접 만들고 있습니다. 이 코드는 내부와 동작이 유사할 수 있지만 리팩토링을 하거나 코드를 수정할 때 도움이 된다고 생각합니다.