Skip to content

Commit

Permalink
test: OptionServiceTest 수정
Browse files Browse the repository at this point in the history
- OptionFactory 컴포넌트화
- OptionServiceTest save 검증 수정
  • Loading branch information
devholic22 committed Feb 23, 2024
1 parent 5b70292 commit 1e58309
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@
import com.atwoz.member.domain.info.option.Option;
import com.atwoz.member.domain.info.option.Religion;
import com.atwoz.member.domain.info.option.Smoke;
import org.springframework.stereotype.Component;

@Component
public class OptionFactory {

public static Option fromRequest(final Long memberId, final OptionWriteRequest request) {
public Option fromRequest(final Long memberId, final OptionWriteRequest request) {

Smoke smoke = Smoke.findByName(request.smoke());
Religion religion = Religion.findByName(request.religion());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,11 @@
public class OptionService {

private final OptionRepository optionRepository;
private final OptionFactory optionFactory;

@Transactional
public void writeOption(final Long memberId, final OptionWriteRequest request) {
Option newOption = OptionFactory.fromRequest(memberId, request);
Option newOption = optionFactory.fromRequest(memberId, request);
if (!isMemberOptionExist(memberId)) {
optionRepository.save(newOption);
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.assertj.core.api.SoftAssertions.assertSoftly;

import com.atwoz.member.application.info.dto.option.OptionWriteRequest;
import com.atwoz.member.application.info.option.OptionFactory;
Expand All @@ -16,7 +17,6 @@
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.junit.jupiter.MockitoExtension;
import java.util.Optional;

@DisplayNameGeneration(DisplayNameGenerator.ReplaceUnderscores.class)
@SuppressWarnings("NonAsciiCharacters")
Expand All @@ -25,11 +25,13 @@ public class OptionServiceTest {

private OptionService optionService;
private OptionRepository optionRepository;
private OptionFactory optionFactory;

@BeforeEach
void init() {
optionFactory = new OptionFactory();
optionRepository = new OptionFakeRepository();
optionService = new OptionService(optionRepository);
optionService = new OptionService(optionRepository, optionFactory);
}

@Test
Expand All @@ -43,13 +45,17 @@ void init() {
String mbti = "INFJ";

OptionWriteRequest request = new OptionWriteRequest(drink, graduate, religion, smoke, mbti);
Option expectedOption = optionFactory.fromRequest(memberId, request);

// when
optionService.writeOption(memberId, request);

// then
Optional<Option> memberOption = optionRepository.findByMemberId(memberId);
assertThat(memberOption).isPresent();
assertSoftly(softly -> {
softly.assertThat(optionRepository.isExistMemberOption(memberId)).isTrue();
softly.assertThat(optionRepository.findByMemberId(memberId)).isPresent();
softly.assertThat(optionRepository.findByMemberId(memberId).get()).isEqualTo(expectedOption);
});
}

@Test
Expand All @@ -63,8 +69,9 @@ void init() {
String mbti = "INFJ";

OptionWriteRequest request = new OptionWriteRequest(drink, graduate, religion, smoke, mbti);
Option expectedOption = OptionFactory.fromRequest(memberId, request);

optionService.writeOption(memberId, request);
Option expectedOption = optionFactory.fromRequest(memberId, request);

// when
Option memberOption = optionService.findByMemberId(memberId);
Expand Down

0 comments on commit 1e58309

Please sign in to comment.