Skip to content

Commit

Permalink
[Test] Interest 도메인 테스트를 작성한다 (#88)
Browse files Browse the repository at this point in the history
test: Interest 도메인 테스트를 작성한다
  • Loading branch information
vectorch9 authored Nov 4, 2023
1 parent 0ec7be4 commit cbef9dc
Show file tree
Hide file tree
Showing 4 changed files with 145 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/main/java/daybyquest/interest/domain/Interests.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public void save(final Interest interest) {
interestRepository.save(interest);
}

public void validateNotExistentByName(final String interestName) {
private void validateNotExistentByName(final String interestName) {
if (interestRepository.existsByName(interestName)) {
throw new InvalidDomainException(ALREADY_EXIST_INTEREST);
}
Expand Down
18 changes: 18 additions & 0 deletions src/test/java/daybyquest/interest/domain/InterestTest.java
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 src/test/java/daybyquest/interest/domain/InterestsTest.java
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 src/test/java/daybyquest/support/fixture/InterestFixtures.java
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);
}
}

0 comments on commit cbef9dc

Please sign in to comment.