Skip to content

Commit

Permalink
refactor: #478 지역 관련 테스트 코드 리팩토링 (#484)
Browse files Browse the repository at this point in the history
* test: RegionTest 리팩토링

* test: AuctionRegionTest 리팩토링

* test: RestTemplateInitRegionProcessorTest Fixture 추가 및 테스트 케이스 리팩토링

* test: JpaRegionRepositoryTest Fixture 추가 및 테스트 케이스 리팩토링

* test: RegionServiceTest Fixture 추가 및 테스트 케이스 리팩토링

* rename: 잘못된 메서드 이름 변경

* test: RegionControllerTest Fixture 추가 및 테스트 케이스 리팩토링

* test: 컨트롤러에서 사용하는 픽스처 객체의 아이디가 중복되지 않도록 변경

* test: 원하는 테스트 결과인지 아이디를 통해 비교하도록 변경

* style: 개행 수정

* test: 모든 픽스처 객체를 `@BeforeEach`로 세팅하도록 변경

* test: 실패하는 테스트 케이스 수정

* test: 테스트 픽스처 추가, 네이밍 변경, 코드 스타일 개선

* test: 픽스처 네이밍 변경 및 접근 제어자 변경
  • Loading branch information
kwonyj1022 authored and apptie committed Oct 7, 2023
1 parent 4b9673b commit 2639865
Show file tree
Hide file tree
Showing 12 changed files with 407 additions and 434 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@
import com.ddang.ddang.region.application.RegionService;
import com.ddang.ddang.region.application.dto.ReadRegionDto;
import com.ddang.ddang.region.presentation.dto.response.ReadRegionResponse;
import java.util.List;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/regions")
@RequiredArgsConstructor
Expand Down Expand Up @@ -39,7 +40,7 @@ public ResponseEntity<List<ReadRegionResponse>> readAllSecond(@PathVariable fina
}

@GetMapping("/{firstId}/{secondId}")
public ResponseEntity<List<ReadRegionResponse>> readAllSecond(
public ResponseEntity<List<ReadRegionResponse>> readAllThird(
@PathVariable final Long firstId,
@PathVariable final Long secondId
) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
package com.ddang.ddang.region.application;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.mockito.BDDMockito.given;

import com.ddang.ddang.region.application.dto.ReadRegionDto;
import com.ddang.ddang.region.application.exception.RegionNotFoundException;
import com.ddang.ddang.region.application.fixture.RegionServiceFixture;
import com.ddang.ddang.region.domain.InitializationRegionProcessor;
import com.ddang.ddang.region.domain.Region;
import com.ddang.ddang.region.infrastructure.persistence.JpaRegionRepository;
import java.util.List;
import org.assertj.core.api.SoftAssertions;
import org.junit.jupiter.api.DisplayNameGeneration;
import org.junit.jupiter.api.DisplayNameGenerator;
Expand All @@ -19,11 +15,16 @@
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.mockito.BDDMockito.given;

@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@Transactional
@DisplayNameGeneration(DisplayNameGenerator.ReplaceUnderscores.class)
@SuppressWarnings("NonAsciiCharacters")
class RegionServiceTest {
class RegionServiceTest extends RegionServiceFixture {

@MockBean
InitializationRegionProcessor regionProcessor;
Expand All @@ -37,127 +38,86 @@ class RegionServiceTest {
@Test
void 대한민국_전국의_지역을_초기화한다() {
// given
final Region firstRegion = new Region("서울특별시");
final Region secondRegion = new Region("송파구");
final Region thirdRegion = new Region("가락1동");

secondRegion.addThirdRegion(thirdRegion);
firstRegion.addSecondRegion(secondRegion);
given(regionProcessor.requestRegions()).willReturn(List.of(firstRegion));
given(regionProcessor.requestRegions()).willReturn(List.of(서울특별시, 두번째_지역이_없는_첫번째_지역));

// when
regionService.createRegions();

// then
final List<Region> actual = regionRepository.findAll();

SoftAssertions.assertSoftly(softAssertions -> {
softAssertions.assertThat(actual).hasSize(3);

softAssertions.assertThat(actual.get(0)).isEqualTo(firstRegion);
softAssertions.assertThat(actual.get(0).getSecondRegions()).hasSize(1);

final Region actualSecondRegion = actual.get(0).getSecondRegions().get(0);
softAssertions.assertThat(actualSecondRegion).isEqualTo(secondRegion);
softAssertions.assertThat(actualSecondRegion.getThirdRegions()).hasSize(1);
final Region actualFirstRegion1 = actual.get(0);
final Region actualFirstRegion2 = actual.get(5);
final Region actualSecondRegion1OfFirstRegion1 = actualFirstRegion1.getSecondRegions().get(0);
final Region actualSecondRegion2OfFirstRegion1 = actualFirstRegion1.getSecondRegions().get(1);
final Region actualThirdRegion1OfSecondRegion1 = actualSecondRegion1OfFirstRegion1.getThirdRegions().get(0);
final Region actualThirdRegion2OfSecondRegion1 = actualSecondRegion1OfFirstRegion1.getThirdRegions().get(1);

final Region actualThirdRegion = actualSecondRegion.getThirdRegions().get(0);
softAssertions.assertThat(actualThirdRegion).isEqualTo(thirdRegion);
SoftAssertions.assertSoftly(softAssertions -> {
softAssertions.assertThat(actual).hasSize(6);
softAssertions.assertThat(actualFirstRegion1).isEqualTo(서울특별시);
softAssertions.assertThat(actualFirstRegion2).isEqualTo(두번째_지역이_없는_첫번째_지역);
softAssertions.assertThat(actualFirstRegion1.getSecondRegions()).hasSize(2);
softAssertions.assertThat(actualSecondRegion1OfFirstRegion1).isEqualTo(서울특별시_강남구);
softAssertions.assertThat(actualSecondRegion2OfFirstRegion1).isEqualTo(세번째_지역이_없는_두번째_지역);
softAssertions.assertThat(actualSecondRegion1OfFirstRegion1.getThirdRegions()).hasSize(2);
softAssertions.assertThat(actualThirdRegion1OfSecondRegion1).isEqualTo(서울특별시_강남구_삼성동);
softAssertions.assertThat(actualThirdRegion2OfSecondRegion1).isEqualTo(서울특별시_강남구_대치동);
});
}

@Test
void 모든_첫번째_지역을_조회한다() {
// given
final Region first1 = new Region("first1");
final Region first2 = new Region("first2");
final Region second = new Region("second");

first1.addSecondRegion(second);

regionRepository.save(first1);
regionRepository.save(first2);

// when
final List<ReadRegionDto> actual = regionService.readAllFirst();

// then
assertThat(actual).hasSize(2);
}

@Test
void 첫번째_지역이_없는_경우_지역_조회시_예외가_발생한다() {
// when & then
assertThatThrownBy(() -> regionService.readAllFirst())
.isInstanceOf(RegionNotFoundException.class)
.hasMessage("등록된 지역이 없습니다.");
SoftAssertions.assertSoftly(softAssertions -> {
softAssertions.assertThat(actual).hasSize(2);
softAssertions.assertThat(actual.get(0).id()).isEqualTo(서울특별시.getId());
softAssertions.assertThat(actual.get(1).id()).isEqualTo(두번째_지역이_없는_첫번째_지역.getId());
});
}

@Test
void 첫번째_지역에_해당하는_모든_두번째_지역을_조회한다() {
// given
final Region first = new Region("first");
final Region second1 = new Region("second1");
final Region second2 = new Region("second2");

first.addSecondRegion(second1);
first.addSecondRegion(second2);

regionRepository.save(first);

// when
final List<ReadRegionDto> actual = regionService.readAllSecondByFirstRegionId(first.getId());
final List<ReadRegionDto> actual = regionService.readAllSecondByFirstRegionId(서울특별시.getId());

// then
assertThat(actual).hasSize(2);
SoftAssertions.assertSoftly(softAssertions -> {
softAssertions.assertThat(actual).hasSize(2);
softAssertions.assertThat(actual.get(0).id()).isEqualTo(서울특별시_강남구.getId());
softAssertions.assertThat(actual.get(1).id()).isEqualTo(세번째_지역이_없는_두번째_지역.getId());
});
}

@Test
void 지정한_첫번째_지역에_해당하는_두번째_지역이_없는_경우_두번째_지역_조회시_예외가_발생한다() {
// given
final Region first = new Region("first");

regionRepository.save(first);

// when & then
assertThatThrownBy(() -> regionService.readAllSecondByFirstRegionId(first.getId()))
assertThatThrownBy(() -> regionService.readAllSecondByFirstRegionId(두번째_지역이_없는_첫번째_지역.getId()))
.isInstanceOf(RegionNotFoundException.class)
.hasMessage("지정한 첫 번째 지역에 해당하는 두 번째 지역이 없습니다.");
}

@Test
void 두번째_지역에_해당하는_모든_세번째_지역을_조회한다() {
// given
final Region first = new Region("first");
final Region second = new Region("second");
final Region third1 = new Region("third1");
final Region third2 = new Region("third2");

first.addSecondRegion(second);
second.addThirdRegion(third1);
second.addThirdRegion(third2);

regionRepository.save(first);

// when
final List<ReadRegionDto> actual = regionService.readAllThirdByFirstAndSecondRegionId(first.getId(), second.getId());
final List<ReadRegionDto> actual =
regionService.readAllThirdByFirstAndSecondRegionId(서울특별시.getId(), 서울특별시_강남구.getId());

// then
assertThat(actual).hasSize(2);
SoftAssertions.assertSoftly(softAssertions -> {
softAssertions.assertThat(actual).hasSize(2);
softAssertions.assertThat(actual.get(0).id()).isEqualTo(서울특별시_강남구_삼성동.getId());
softAssertions.assertThat(actual.get(1).id()).isEqualTo(서울특별시_강남구_대치동.getId());
});
}

@Test
void 지정한_첫번째와_두번째_지역에_해당하는_세번째_지역이_없는_경우_세번째_지역_조회시_예외가_발생한다() {
// given
final Region first = new Region("first");
final Region second = new Region("second");

first.addSecondRegion(second);

regionRepository.save(first);

// when & then
assertThatThrownBy(() -> regionService.readAllThirdByFirstAndSecondRegionId(first.getId(), second.getId()))
assertThatThrownBy(() -> regionService.readAllThirdByFirstAndSecondRegionId(서울특별시.getId(), 세번째_지역이_없는_두번째_지역.getId()))
.isInstanceOf(RegionNotFoundException.class)
.hasMessage("지정한 첫 번째와 두 번째 지역에 해당하는 세 번째 지역이 없습니다.");
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package com.ddang.ddang.region.application.fixture;

import com.ddang.ddang.region.domain.Region;
import com.ddang.ddang.region.infrastructure.persistence.JpaRegionRepository;
import org.junit.jupiter.api.BeforeEach;
import org.springframework.beans.factory.annotation.Autowired;

@SuppressWarnings("NonAsciiCharacters")
public class RegionServiceFixture {

@Autowired
private JpaRegionRepository regionRepository;

protected Region 서울특별시;
protected Region 두번째_지역이_없는_첫번째_지역;
protected Region 서울특별시_강남구;
protected Region 세번째_지역이_없는_두번째_지역;
protected Region 서울특별시_강남구_삼성동;
protected Region 서울특별시_강남구_대치동;

@BeforeEach
void setUp() {
서울특별시 = new Region("서울특별시");
두번째_지역이_없는_첫번째_지역 = new Region("두번째 지역이 없는 첫번째 지역");
서울특별시_강남구 = new Region("강남구");
세번째_지역이_없는_두번째_지역 = new Region("세번째 지역이 없는 두번째 지역");
서울특별시_강남구_삼성동 = new Region("삼성동");
서울특별시_강남구_대치동 = new Region("대치동");

서울특별시.addSecondRegion(서울특별시_강남구);
서울특별시.addSecondRegion(세번째_지역이_없는_두번째_지역);

서울특별시_강남구.addThirdRegion(서울특별시_강남구_삼성동);
서울특별시_강남구.addThirdRegion(서울특별시_강남구_대치동);

regionRepository.save(서울특별시);
regionRepository.save(두번째_지역이_없는_첫번째_지역);
}
}
Original file line number Diff line number Diff line change
@@ -1,36 +1,25 @@
package com.ddang.ddang.region.domain;

import static org.assertj.core.api.Assertions.assertThat;

import com.ddang.ddang.auction.domain.Auction;
import com.ddang.ddang.region.domain.fixture.AuctionRegionFixture;
import org.junit.jupiter.api.DisplayNameGeneration;
import org.junit.jupiter.api.DisplayNameGenerator;
import org.junit.jupiter.api.Test;

import static org.assertj.core.api.Assertions.assertThat;

@DisplayNameGeneration(DisplayNameGenerator.ReplaceUnderscores.class)
@SuppressWarnings("NonAsciiCharacters")
class AuctionRegionTest {
class AuctionRegionTest extends AuctionRegionFixture {

@Test
void 직거래_지역과_경매의_연관관계를_세팅한다() {
// given
final Region firstRegion = new Region("서울특별시");
final Region secondRegion = new Region("강남구");
final Region thirdRegion = new Region("역삼동");

secondRegion.addThirdRegion(thirdRegion);
firstRegion.addSecondRegion(secondRegion);

final AuctionRegion auctionRegion = new AuctionRegion(firstRegion);

final Auction auction = Auction.builder()
.title("title")
.build();
final AuctionRegion auctionRegion = new AuctionRegion(서울특별시_하위_강남구_하위_역삼동);

// when
auctionRegion.initAuction(auction);
auctionRegion.initAuction(경매);

// then
assertThat(auctionRegion.getAuction()).isNotNull();
assertThat(auctionRegion.getAuction().getTitle()).isEqualTo(경매.getTitle());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,9 @@ class RegionTest {

// then
SoftAssertions.assertSoftly(softAssertions -> {
softAssertions.assertThat(first.getSecondRegions())
.hasSize(1);
softAssertions.assertThat(second.getFirstRegion())
.isNotNull();
softAssertions.assertThat(first.getSecondRegions()).hasSize(1);
softAssertions.assertThat(first.getSecondRegions().get(0).getName()).isEqualTo(second.getName());
softAssertions.assertThat(second.getFirstRegion().getName()).isEqualTo(first.getName());
});
}

Expand All @@ -41,12 +40,10 @@ class RegionTest {

// then
SoftAssertions.assertSoftly(softAssertions -> {
softAssertions.assertThat(second.getThirdRegions())
.hasSize(1);
softAssertions.assertThat(third.getSecondRegion())
.isNotNull();
softAssertions.assertThat(third.getFirstRegion())
.isNotNull();
softAssertions.assertThat(second.getThirdRegions()).hasSize(1);
softAssertions.assertThat(second.getThirdRegions().get(0).getName()).isEqualTo(third.getName());
softAssertions.assertThat(third.getSecondRegion().getName()).isEqualTo(second.getName());
softAssertions.assertThat(third.getFirstRegion().getName()).isEqualTo(first.getName());
});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.ddang.ddang.region.domain.fixture;

import com.ddang.ddang.auction.domain.Auction;
import com.ddang.ddang.region.domain.Region;
import org.junit.jupiter.api.BeforeEach;

@SuppressWarnings("NonAsciiCharacters")
public class AuctionRegionFixture {

protected Region 서울특별시_하위_강남구_하위_역삼동;
protected Auction 경매;

@BeforeEach
void setUp() {
Region 서울특별시 = new Region("서울특별시");
Region 서울특별시_하위_강남구 = new Region("강남구");
서울특별시_하위_강남구_하위_역삼동 = new Region("역삼동");

서울특별시_하위_강남구.addThirdRegion(서울특별시_하위_강남구_하위_역삼동);
서울특별시.addSecondRegion(서울특별시_하위_강남구);

경매 = Auction.builder()
.title("title")
.build();
}
}
Loading

0 comments on commit 2639865

Please sign in to comment.