-
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: Interest 도메인 테스트를 작성한다
- Loading branch information
Showing
4 changed files
with
145 additions
and
1 deletion.
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
18 changes: 18 additions & 0 deletions
18
src/test/java/daybyquest/interest/domain/InterestTest.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,18 @@ | ||
package daybyquest.interest.domain; | ||
|
||
import static org.assertj.core.api.Assertions.assertThatThrownBy; | ||
|
||
import daybyquest.global.error.exception.InvalidDomainException; | ||
import daybyquest.image.vo.Image; | ||
import org.junit.jupiter.params.ParameterizedTest; | ||
import org.junit.jupiter.params.provider.ValueSource; | ||
|
||
public class InterestTest { | ||
|
||
@ParameterizedTest | ||
@ValueSource(strings = {"", "일이삼사오육칠팔구십일이삼사오육칠팔구십일"}) | ||
void 이름이_1에서_20글자가_아니면_예외를_던진다(final String name) { | ||
assertThatThrownBy(() -> new Interest(name, new Image("사진"))) | ||
.isInstanceOf(InvalidDomainException.class); | ||
} | ||
} |
97 changes: 97 additions & 0 deletions
97
src/test/java/daybyquest/interest/domain/InterestsTest.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,97 @@ | ||
package daybyquest.interest.domain; | ||
|
||
import static daybyquest.support.fixture.InterestFixtures.INTERST_1; | ||
import static daybyquest.support.fixture.InterestFixtures.INTERST_2; | ||
import static daybyquest.support.fixture.InterestFixtures.INTERST_3; | ||
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.InvalidDomainException; | ||
import daybyquest.global.error.exception.NotExistInterestException; | ||
import java.util.List; | ||
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 InterestsTest { | ||
|
||
@Mock | ||
private InterestRepository interestRepository; | ||
|
||
@InjectMocks | ||
private Interests interests; | ||
|
||
@Test | ||
void 이름_중복_검사를_하고_저장한다() { | ||
// given & when | ||
interests.save(INTERST_1.생성()); | ||
|
||
// then | ||
assertAll(() -> { | ||
then(interestRepository).should().existsByName(INTERST_1.name); | ||
then(interestRepository).should().save(any(Interest.class)); | ||
}); | ||
} | ||
|
||
@Test | ||
void 저장_시_이름이_이미_있다면_예외를_던진다() { | ||
// given | ||
given(interestRepository.existsByName(INTERST_1.name)).willReturn(true); | ||
|
||
// then | ||
assertThatThrownBy(() -> interests.save(INTERST_1.생성())) | ||
.isInstanceOf(InvalidDomainException.class); | ||
} | ||
|
||
@Test | ||
void 이름을_통해_존재_여부를_검증한다() { | ||
// given | ||
given(interestRepository.existsByName(INTERST_1.name)).willReturn(true); | ||
|
||
// when | ||
interests.validateInterest(INTERST_1.name); | ||
|
||
// then | ||
then(interestRepository).should().existsByName(INTERST_1.name); | ||
} | ||
|
||
@Test | ||
void 이름을_통해_존재_여부를_검증_시_존재한다면_에외를_던진다() { | ||
// given & when & then | ||
assertThatThrownBy(() -> interests.validateInterest(INTERST_1.name)) | ||
.isInstanceOf(NotExistInterestException.class); | ||
} | ||
|
||
@Test | ||
void 여러_이름을_통해_존재_여부를_검증한다() { | ||
// given | ||
final List<String> names = List.of(INTERST_1.name, INTERST_2.name, INTERST_3.name); | ||
final List<Interest> expected = List.of(INTERST_1.생성(), INTERST_2.생성(), INTERST_3.생성()); | ||
given(interestRepository.findAllByNameIn(names)).willReturn(expected); | ||
|
||
// when | ||
interests.validateInterests(names); | ||
|
||
// then | ||
then(interestRepository).should().findAllByNameIn(names); | ||
} | ||
|
||
|
||
@Test | ||
void 여러_이름을_통해_존재_여부를_검증_시_하나라도_없다면_예외를_던진다() { | ||
// given | ||
final List<String> names = List.of(INTERST_1.name, INTERST_2.name, INTERST_3.name); | ||
final List<Interest> expected = List.of(INTERST_1.생성(), INTERST_2.생성()); | ||
given(interestRepository.findAllByNameIn(names)).willReturn(expected); | ||
|
||
// when | ||
assertThatThrownBy(() -> interests.validateInterests(names)) | ||
.isInstanceOf(NotExistInterestException.class); | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
src/test/java/daybyquest/support/fixture/InterestFixtures.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,29 @@ | ||
package daybyquest.support.fixture; | ||
|
||
import daybyquest.image.vo.Image; | ||
import daybyquest.interest.domain.Interest; | ||
|
||
public enum InterestFixtures { | ||
|
||
INTERST_1("관심사1", "관심사사진1"), | ||
INTERST_2("관심사2", "관심사사진2"), | ||
INTERST_3("관심사3", "관심사사진3"), | ||
INTERST_4("관심사4", "관심사사진4"); | ||
|
||
public final String name; | ||
|
||
public final String imageIdentifier; | ||
|
||
InterestFixtures(final String name, final String imageIdentifier) { | ||
this.name = name; | ||
this.imageIdentifier = imageIdentifier; | ||
} | ||
|
||
public Interest 생성() { | ||
return new Interest(name, 사진()); | ||
} | ||
|
||
public Image 사진() { | ||
return new Image(imageIdentifier); | ||
} | ||
} |