Skip to content

Commit

Permalink
test: HobbyService 테스트 작성
Browse files Browse the repository at this point in the history
- HobbyService 테스트 작성
- HobbyFakeRepository 작성
- HobbyService 메서드 이름 수정
  • Loading branch information
devholic22 committed Feb 23, 2024
1 parent f66f1f4 commit 18db8df
Show file tree
Hide file tree
Showing 3 changed files with 114 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public class HobbyService {

@Transactional
public void saveMemberHobbies(final Long memberId, final List<String> hobbyNames) {
deleteBeforeMemberHobbies(memberId);
deleteMemberHobbies(memberId);
List<Hobby> memberHobbies = HobbyName.findAllByNames(hobbyNames)
.stream()
.map(hobbyName -> new Hobby(memberId, hobbyName))
Expand All @@ -27,7 +27,7 @@ public void saveMemberHobbies(final Long memberId, final List<String> hobbyNames
}

@Transactional
public void deleteBeforeMemberHobbies(final Long memberId) {
public void deleteMemberHobbies(final Long memberId) {
hobbyRepository.deleteHobbiesByMemberId(memberId);
}

Expand Down
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);
}
}
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();
}
}

0 comments on commit 18db8df

Please sign in to comment.