-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* test: 뱃지 도메인 테스트 작성 * test: 특정 길이의 문자열 생성 로직 분리 * refactor: eqauls() 메서드 내 instanceof를 도입하여 로직 간단화 * test: Badges 테스트 작성 * test: Owning 도메인 테스트 작성 * test: 뱃지 쿼리 테스트 작성 * test: 쿼리 테스트 시 where 조건에 대한 확인도 고려 * test: 사용하지 않는 변수 제거
- Loading branch information
Showing
14 changed files
with
323 additions
and
22 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package daybyquest.badge.domain; | ||
|
||
import static daybyquest.support.fixture.BadgeFixtures.BADGE_1; | ||
import static org.assertj.core.api.Assertions.assertThatThrownBy; | ||
|
||
import daybyquest.global.error.exception.InvalidDomainException; | ||
import org.junit.jupiter.params.ParameterizedTest; | ||
import org.junit.jupiter.params.provider.ValueSource; | ||
|
||
public class BadgeTest { | ||
|
||
@ParameterizedTest | ||
@ValueSource(strings = {"", "일이삼사오육칠팔구십일이삼사오육"}) | ||
void 뱃지_이름이_1_에서_15_글자가_아니면_예외를_던진다(final String name) { | ||
// given & when & then | ||
assertThatThrownBy(() -> new Badge(name, BADGE_1.사진())) | ||
.isInstanceOf(InvalidDomainException.class); | ||
} | ||
} |
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,80 @@ | ||
package daybyquest.badge.domain; | ||
|
||
import static daybyquest.support.fixture.BadgeFixtures.BADGE_1; | ||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.assertj.core.api.Assertions.assertThatThrownBy; | ||
import static org.junit.jupiter.api.Assertions.assertAll; | ||
import static org.mockito.ArgumentMatchers.any; | ||
import static org.mockito.BDDMockito.given; | ||
import static org.mockito.BDDMockito.then; | ||
|
||
import daybyquest.global.error.exception.NotExistBadgeException; | ||
import java.util.Optional; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
import org.mockito.InjectMocks; | ||
import org.mockito.Mock; | ||
import org.mockito.junit.jupiter.MockitoExtension; | ||
|
||
@ExtendWith(MockitoExtension.class) | ||
public class BadgesTest { | ||
|
||
@Mock | ||
private BadgeRepository badgeRepository; | ||
@InjectMocks | ||
private Badges badges; | ||
|
||
@Test | ||
void 뱃지를_저장한다() { | ||
// given & when | ||
badges.save(BADGE_1.생성()); | ||
|
||
// then | ||
then(badgeRepository).should().save(any(Badge.class)); | ||
} | ||
|
||
@Test | ||
void 뱃지_ID_존재_여부를_검증한다() { | ||
// given | ||
final Long badgeId = 1L; | ||
given(badgeRepository.existsById(badgeId)).willReturn(true); | ||
|
||
// when | ||
badges.validateExistentById(badgeId); | ||
|
||
// then | ||
then(badgeRepository).should().existsById(badgeId); | ||
} | ||
|
||
@Test | ||
void 뱃지_ID_존재_여부_검증_시_없다면_예외를_던진다() { | ||
// given & when & then | ||
assertThatThrownBy(() -> badges.validateExistentById(1L)) | ||
.isInstanceOf(NotExistBadgeException.class); | ||
} | ||
|
||
@Test | ||
void ID를_통해_뱃지를_조회한다() { | ||
// given | ||
final Long badgeId = 1L; | ||
given(badgeRepository.findById(badgeId)).willReturn(Optional.of(BADGE_1.생성(badgeId))); | ||
|
||
// when | ||
final Badge actual = badges.getById(badgeId); | ||
|
||
// then | ||
assertAll(() -> { | ||
then(badgeRepository).should().findById(badgeId); | ||
assertThat(actual.getId()).isEqualTo(badgeId); | ||
assertThat(actual.getName()).isEqualTo(BADGE_1.name); | ||
assertThat(actual.getImage()).isEqualTo(BADGE_1.사진()); | ||
}); | ||
} | ||
|
||
@Test | ||
void ID를_통한_조회_시_없다면_예외를_던진다() { | ||
// given & when & then | ||
assertThatThrownBy(() -> badges.getById(1L)) | ||
.isInstanceOf(NotExistBadgeException.class); | ||
} | ||
} |
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,57 @@ | ||
package daybyquest.badge.domain; | ||
|
||
import static daybyquest.support.fixture.BadgeFixtures.BADGE_1; | ||
import static org.mockito.ArgumentMatchers.any; | ||
import static org.mockito.BDDMockito.given; | ||
import static org.mockito.BDDMockito.then; | ||
|
||
import daybyquest.user.domain.Users; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
import org.mockito.InjectMocks; | ||
import org.mockito.Mock; | ||
import org.mockito.junit.jupiter.MockitoExtension; | ||
|
||
@ExtendWith(MockitoExtension.class) | ||
public class OwningsTest { | ||
|
||
@Mock | ||
private OwningRepository owningRepository; | ||
|
||
@Mock | ||
private Badges badges; | ||
|
||
@Mock | ||
private Users users; | ||
|
||
@InjectMocks | ||
private Ownings ownings; | ||
|
||
@Test | ||
void 사용자_ID와_뱃지_ID를_통해_소유를_저장한다() { | ||
// given | ||
final Long userId = 1L; | ||
final Long badgeId = 2L; | ||
given(badges.getById(badgeId)).willReturn(BADGE_1.생성(badgeId)); | ||
|
||
// when | ||
ownings.saveByUserIdAndBadgeId(userId, badgeId); | ||
|
||
// then | ||
then(owningRepository).should().save(any(Owning.class)); | ||
} | ||
|
||
@Test | ||
void 저장_시_사용자_ID_존재_여부를_검증한다() { | ||
// given | ||
final Long userId = 1L; | ||
final Long badgeId = 2L; | ||
given(badges.getById(badgeId)).willReturn(BADGE_1.생성(badgeId)); | ||
|
||
// when | ||
ownings.saveByUserIdAndBadgeId(userId, badgeId); | ||
|
||
// then | ||
then(users).should().validateExistentById(userId); | ||
} | ||
} |
87 changes: 87 additions & 0 deletions
87
src/test/java/daybyquest/badge/query/BadgeDaoQuerydslImplTest.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,87 @@ | ||
package daybyquest.badge.query; | ||
|
||
import static daybyquest.support.fixture.BadgeFixtures.BADGE_1; | ||
import static daybyquest.support.fixture.BadgeFixtures.BADGE_2; | ||
import static daybyquest.support.fixture.BadgeFixtures.BADGE_3; | ||
import static daybyquest.support.fixture.UserFixtures.BOB; | ||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.junit.jupiter.api.Assertions.assertAll; | ||
|
||
import daybyquest.badge.domain.Badge; | ||
import daybyquest.badge.domain.Owning; | ||
import daybyquest.global.query.NoOffsetTimePage; | ||
import daybyquest.support.test.QuerydslTest; | ||
import daybyquest.user.domain.User; | ||
import java.util.List; | ||
import org.junit.jupiter.api.Test; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.context.annotation.Import; | ||
|
||
@Import(BadgeDaoQuerydslImpl.class) | ||
public class BadgeDaoQuerydslImplTest extends QuerydslTest { | ||
|
||
@Autowired | ||
private BadgeDaoQuerydslImpl badgeDao; | ||
|
||
@Test | ||
void 소유한_뱃지_목록을_조회한다() { | ||
// given | ||
final User bob = 저장한다(BOB.생성()); | ||
final Badge badge1 = 저장한다(BADGE_1.생성()); | ||
final Badge badge2 = 저장한다(BADGE_2.생성()); | ||
final Badge badge3 = 저장한다(BADGE_3.생성()); | ||
|
||
저장한다(new Owning(bob.getId(), badge1)); | ||
저장한다(new Owning(bob.getId(), badge2)); | ||
저장한다(new Owning(bob.getId(), badge3)); | ||
|
||
final NoOffsetTimePage page = new NoOffsetTimePage(null, 5); | ||
|
||
// when | ||
final List<BadgeData> badgeData = badgeDao.getBadgePageByUserIds(bob.getId(), page); | ||
|
||
// then | ||
assertThat(badgeData).hasSize(3); | ||
} | ||
|
||
@Test | ||
void 소유하지_않은_뱃지는_목록에_포함되지_않는다() { | ||
// given | ||
final User bob = 저장한다(BOB.생성()); | ||
final Badge badge1 = 저장한다(BADGE_1.생성()); | ||
저장한다(BADGE_2.생성()); | ||
저장한다(BADGE_3.생성()); | ||
|
||
저장한다(new Owning(bob.getId(), badge1)); | ||
|
||
final NoOffsetTimePage page = new NoOffsetTimePage(null, 5); | ||
|
||
// when | ||
final List<BadgeData> badgeData = badgeDao.getBadgePageByUserIds(bob.getId(), page); | ||
|
||
// then | ||
assertThat(badgeData).hasSize(1); | ||
} | ||
|
||
@Test | ||
void 뱃지_데이터는_뱃지_정보와_일치_해야한다() { | ||
// given | ||
final User bob = 저장한다(BOB.생성()); | ||
final Badge badge1 = 저장한다(BADGE_1.생성()); | ||
|
||
저장한다(new Owning(bob.getId(), badge1)); | ||
|
||
final NoOffsetTimePage page = new NoOffsetTimePage(null, 5); | ||
|
||
// when | ||
final List<BadgeData> badgeData = badgeDao.getBadgePageByUserIds(bob.getId(), page); | ||
|
||
// then | ||
assertAll(() -> { | ||
assertThat(badgeData).hasSize(1); | ||
final BadgeData actual = badgeData.get(0); | ||
assertThat(actual.getName()).isEqualTo(BADGE_1.name); | ||
assertThat(actual.getImageIdentifier()).isEqualTo(BADGE_1.imageIdentifier); | ||
}); | ||
} | ||
} |
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
Oops, something went wrong.