-
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.
- HobbyService 테스트 작성 - HobbyFakeRepository 작성 - HobbyService 메서드 이름 수정
- Loading branch information
1 parent
f66f1f4
commit 18db8df
Showing
3 changed files
with
114 additions
and
2 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
72 changes: 72 additions & 0 deletions
72
src/test/java/com/atwoz/member/application/info/HobbyServiceTest.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,72 @@ | ||
package com.atwoz.member.application.info; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
import com.atwoz.member.application.info.hobby.HobbyService; | ||
import com.atwoz.member.domain.info.hobby.Hobby; | ||
import com.atwoz.member.domain.info.hobby.HobbyRepository; | ||
import com.atwoz.member.infrastructure.info.HobbyFakeRepository; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.DisplayNameGeneration; | ||
import org.junit.jupiter.api.DisplayNameGenerator; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
import org.mockito.junit.jupiter.MockitoExtension; | ||
import java.util.List; | ||
|
||
@DisplayNameGeneration(DisplayNameGenerator.ReplaceUnderscores.class) | ||
@SuppressWarnings("NonAsciiCharacters") | ||
@ExtendWith(MockitoExtension.class) | ||
public class HobbyServiceTest { | ||
|
||
private HobbyService hobbyService; | ||
private HobbyRepository hobbyRepository; | ||
|
||
@BeforeEach | ||
void init() { | ||
hobbyRepository = new HobbyFakeRepository(); | ||
hobbyService = new HobbyService(hobbyRepository); | ||
} | ||
|
||
@Test | ||
void 회원의_모든_취미를_저장한다() { | ||
// given | ||
Long memberId = 1L; | ||
List<String> hobbyNames = List.of("자전거", "애니메이션"); | ||
|
||
// when | ||
hobbyService.saveMemberHobbies(memberId, hobbyNames); | ||
|
||
// then | ||
List<Hobby> saveHobbies = hobbyRepository.findAllByMemberId(memberId); | ||
assertThat(saveHobbies.size()).isEqualTo(2); | ||
} | ||
|
||
@Test | ||
void 회원의_모든_취미를_삭제한다() { | ||
// given | ||
Long memberId = 1L; | ||
List<String> hobbyNames = List.of("자전거", "애니메이션"); | ||
hobbyService.saveMemberHobbies(memberId, hobbyNames); | ||
|
||
// when | ||
hobbyService.deleteMemberHobbies(memberId); | ||
|
||
// then | ||
assertThat(hobbyRepository.findAllByMemberId(memberId)).isEmpty(); | ||
} | ||
|
||
@Test | ||
void 회원의_모든_취미를_조회한다() { | ||
// given | ||
Long memberId = 1L; | ||
List<String> hobbyNames = List.of("자전거", "애니메이션"); | ||
hobbyService.saveMemberHobbies(memberId, hobbyNames); | ||
|
||
// when | ||
List<Hobby> memberHobbies = hobbyService.findMemberHobbies(memberId); | ||
|
||
// then | ||
assertThat(memberHobbies.size()).isEqualTo(2); | ||
} | ||
} |
40 changes: 40 additions & 0 deletions
40
src/test/java/com/atwoz/member/infrastructure/info/HobbyFakeRepository.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,40 @@ | ||
package com.atwoz.member.infrastructure.info; | ||
|
||
import com.atwoz.member.domain.info.hobby.Hobby; | ||
import com.atwoz.member.domain.info.hobby.HobbyRepository; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
public class HobbyFakeRepository implements HobbyRepository { | ||
|
||
private final Map<Long, Hobby> map = new HashMap<>(); | ||
private long id = 1L; | ||
|
||
@Override | ||
public void save(final Hobby hobby) { | ||
Hobby newHobby = new Hobby(hobby.getMemberId(), hobby.getHobbyName()); | ||
map.put(id, newHobby); | ||
id++; | ||
} | ||
|
||
@Override | ||
public void saveAll(final List<Hobby> memberHobbies) { | ||
memberHobbies.stream() | ||
.map(hobby -> new Hobby(hobby.getMemberId(), hobby.getHobbyName())) | ||
.forEach(hobby -> map.put(id++, hobby)); | ||
} | ||
|
||
@Override | ||
public void deleteHobbiesByMemberId(final Long memberId) { | ||
map.values().removeIf(hobby -> memberId.equals(hobby.getMemberId())); | ||
} | ||
|
||
@Override | ||
public List<Hobby> findAllByMemberId(final Long memberId) { | ||
return map.values() | ||
.stream() | ||
.filter(hobby -> memberId.equals(hobby.getMemberId())) | ||
.toList(); | ||
} | ||
} |