From 538802fa5410fe352ac2240a8155d4a1d1c4d88d Mon Sep 17 00:00:00 2001 From: wwwcomcomcomcom Date: Thu, 5 Sep 2024 18:13:00 +0900 Subject: [PATCH 1/2] =?UTF-8?q?test(oneseo):QueryTestResultService=20?= =?UTF-8?q?=EC=84=9C=EB=B9=84=EC=8A=A4=20=ED=85=8C=EC=8A=A4=ED=8A=B8?= =?UTF-8?q?=EC=BD=94=EB=93=9C=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../service/QueryTestResultServiceTest.java | 113 ++++++++++++++++++ 1 file changed, 113 insertions(+) create mode 100644 src/test/java/team/themoment/hellogsmv3/domain/oneseo/service/QueryTestResultServiceTest.java diff --git a/src/test/java/team/themoment/hellogsmv3/domain/oneseo/service/QueryTestResultServiceTest.java b/src/test/java/team/themoment/hellogsmv3/domain/oneseo/service/QueryTestResultServiceTest.java new file mode 100644 index 00000000..89eb9ff1 --- /dev/null +++ b/src/test/java/team/themoment/hellogsmv3/domain/oneseo/service/QueryTestResultServiceTest.java @@ -0,0 +1,113 @@ +package team.themoment.hellogsmv3.domain.oneseo.service; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Nested; +import org.junit.jupiter.api.Test; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.springframework.http.HttpStatus; +import team.themoment.hellogsmv3.domain.member.dto.response.FoundMemberTestResDto; +import team.themoment.hellogsmv3.domain.member.entity.Member; +import team.themoment.hellogsmv3.domain.member.entity.type.Sex; +import team.themoment.hellogsmv3.domain.member.service.MemberService; +import team.themoment.hellogsmv3.domain.member.service.QueryTestResultService; +import team.themoment.hellogsmv3.domain.oneseo.entity.EntranceTestResult; +import team.themoment.hellogsmv3.domain.oneseo.entity.Oneseo; +import team.themoment.hellogsmv3.domain.oneseo.entity.type.YesNo; +import team.themoment.hellogsmv3.global.exception.error.ExpectedException; + +import java.time.LocalDate; + +import static org.junit.jupiter.api.Assertions.*; +import static org.mockito.BDDMockito.given; + +@DisplayName("QueryTestResultService 클래스의") +public class QueryTestResultServiceTest { + @Mock + private MemberService memberService; + @Mock + private OneseoService oneseoService; + @InjectMocks + private QueryTestResultService queryTestResultService; + + @Nested + @DisplayName("execute 메소드는") + class Describe_execute { + private final Long memberId = 1L; + @Nested + @DisplayName("존재하는 회원 ID가 주어지고") + class Context_with_existing_member_id { + private Member member; + private Long OneseoId = 1L; + @BeforeEach + void setUp() { + member = Member.builder() + .id(memberId) + .name("홍길동") + .birth(LocalDate.of(2009,1, 1)) + .phoneNumber("01012345678") + .sex(Sex.MALE) + .build(); + given(memberService.findByIdOrThrow(memberId)).willReturn(member); + } + @Nested + @DisplayName("해당 회원의 시험 결과가 존재하면") + class Context_with_existing_test_result { + private Oneseo oneseo; + @BeforeEach + void setUp() { + oneseo = Oneseo.builder() + .id(OneseoId) + .member(member) + .entranceTestResult( + EntranceTestResult.builder() + .id(1L) + .firstTestPassYn(YesNo.YES) + .secondTestPassYn(YesNo.YES) + .build() + ) + .build(); + given(oneseoService.findByMemberOrThrow(member)).willReturn(oneseo); + } + @Test + @DisplayName("해당 회원의 시험 결과를 반환한다.") + void it_returns_test_result_of_the_member() { + FoundMemberTestResDto result = queryTestResultService.execute(memberId); + assertEquals(YesNo.YES, result.firstTestPassYn()); + assertEquals(YesNo.YES, result.secondTestPassYn()); + } + } + @Nested + @DisplayName("해당 회원의 시험 결과가 존재하지 않으면") + class Context_with_non_existing_test_result { + @BeforeEach + void setUp() { + given(oneseoService.findByMemberOrThrow(member)).willThrow(new ExpectedException("해당 지원자의 원서를 찾을 수 없습니다. member ID: " + member.getId(), HttpStatus.NOT_FOUND)); + } + @Test + @DisplayName("ExpectedException을 던진다.") + void it_returns_expected_exception() { + ExpectedException exception = assertThrows(ExpectedException.class, () -> queryTestResultService.execute(memberId)); + assertEquals("해당 지원자의 원서를 찾을 수 없습니다. member ID: " + member.getId(), exception.getMessage()); + assertEquals(HttpStatus.NOT_FOUND, exception.getStatusCode()); + } + } + } + @Nested + @DisplayName("존재하지 않는 회원 ID가 주어지면") + class Context_with_non_existing_member_id { + @BeforeEach + void setUp() { + given(memberService.findByIdOrThrow(memberId)).willThrow(new ExpectedException("존재하지 않는 지원자입니다. member ID: " + memberId, HttpStatus.NOT_FOUND)); + } + @Test + @DisplayName("ExpectedException을 던진다.") + void it_throws_expected_exception() { + ExpectedException exception = assertThrows(ExpectedException.class, () -> queryTestResultService.execute(memberId)); + assertEquals("존재하지 않는 지원자입니다. member ID: " + memberId, exception.getMessage()); + assertEquals(HttpStatus.NOT_FOUND, exception.getStatusCode()); + } + } + } +} From 903708a9381f7d0b46dba2045896438a97512478 Mon Sep 17 00:00:00 2001 From: wwwcomcomcomcom Date: Thu, 5 Sep 2024 19:30:25 +0900 Subject: [PATCH 2/2] test(oneseo):Enable Bean Mocking --- .../domain/oneseo/service/QueryTestResultServiceTest.java | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/test/java/team/themoment/hellogsmv3/domain/oneseo/service/QueryTestResultServiceTest.java b/src/test/java/team/themoment/hellogsmv3/domain/oneseo/service/QueryTestResultServiceTest.java index 89eb9ff1..cd475be1 100644 --- a/src/test/java/team/themoment/hellogsmv3/domain/oneseo/service/QueryTestResultServiceTest.java +++ b/src/test/java/team/themoment/hellogsmv3/domain/oneseo/service/QueryTestResultServiceTest.java @@ -6,6 +6,7 @@ import org.junit.jupiter.api.Test; import org.mockito.InjectMocks; import org.mockito.Mock; +import org.mockito.MockitoAnnotations; import org.springframework.http.HttpStatus; import team.themoment.hellogsmv3.domain.member.dto.response.FoundMemberTestResDto; import team.themoment.hellogsmv3.domain.member.entity.Member; @@ -31,6 +32,11 @@ public class QueryTestResultServiceTest { @InjectMocks private QueryTestResultService queryTestResultService; + @BeforeEach + void setUp() { + MockitoAnnotations.openMocks(this); + } + @Nested @DisplayName("execute 메소드는") class Describe_execute {