From e66d46eb2fdc0bcf7b19415bbd59f9beece2d983 Mon Sep 17 00:00:00 2001 From: Srltas Date: Thu, 26 Sep 2024 01:15:42 +0900 Subject: [PATCH] =?UTF-8?q?test:=20=EB=8F=99=EB=84=A4=EC=9D=B8=EC=A6=9D?= =?UTF-8?q?=EC=97=90=EC=84=9C=20DTO=20=EC=82=AC=EC=9A=A9=EC=97=90=20?= =?UTF-8?q?=EB=94=B0=EB=A5=B8=20=ED=85=8C=EC=8A=A4=ED=8A=B8=20=EC=BD=94?= =?UTF-8?q?=EB=93=9C=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../NeighborhoodVerificationServiceTest.java | 51 ++++++++++++++----- 1 file changed, 37 insertions(+), 14 deletions(-) diff --git a/src/test/java/com/srltas/runtogether/application/NeighborhoodVerificationServiceTest.java b/src/test/java/com/srltas/runtogether/application/NeighborhoodVerificationServiceTest.java index 0d7c2b3..0a9ce7c 100644 --- a/src/test/java/com/srltas/runtogether/application/NeighborhoodVerificationServiceTest.java +++ b/src/test/java/com/srltas/runtogether/application/NeighborhoodVerificationServiceTest.java @@ -16,8 +16,12 @@ import org.junit.jupiter.api.extension.ExtendWith; import org.mockito.InjectMocks; import org.mockito.Mock; +import org.mockito.MockedStatic; import org.mockito.junit.jupiter.MockitoExtension; +import com.srltas.runtogether.adapter.in.LocationNeighborhoodVerifyRequest; +import com.srltas.runtogether.application.mappper.LocationNeighborhoodVerifyRequestMapper; +import com.srltas.runtogether.application.mappper.UserNeighborhoodVerifyRequestMapper; import com.srltas.runtogether.domain.exception.NeighborhoodNotFoundException; import com.srltas.runtogether.domain.exception.OutOfNeighborhoodBoundaryException; import com.srltas.runtogether.domain.model.location.DistanceCalculator; @@ -42,17 +46,15 @@ class NeighborhoodVerificationServiceTest { @InjectMocks private NeighborhoodVerificationService neighborhoodVerificationService; - private User user; private String neighborhoodName; - private Location currentLocation; - private Neighborhood neighborhood; + private com.srltas.runtogether.adapter.in.UserNeighborhoodVerifyRequest userNeighborhoodVerifyRequest; + private LocationNeighborhoodVerifyRequest locationNeighborhoodVerifyRequest; @BeforeEach public void setUp() { - user = new User(1L, "testUser"); neighborhoodName = "Gangnam"; - currentLocation = new Location(1L, 1L); - neighborhood = new Neighborhood(neighborhoodName, new Location(37.517347, 127.047382), 7.0, distanceCalculator); + userNeighborhoodVerifyRequest = new com.srltas.runtogether.adapter.in.UserNeighborhoodVerifyRequest(1L, "testUser"); + locationNeighborhoodVerifyRequest = new LocationNeighborhoodVerifyRequest(1L, 1L); } @Nested @@ -61,34 +63,54 @@ class WhenNeighborhoodIsFound { @BeforeEach public void setUp() { + Neighborhood neighborhood = new Neighborhood(neighborhoodName, new Location(1L, 1L), 7.0, distanceCalculator); given(neighborhoodRepository.findByName(neighborhoodName)).willReturn(Optional.of(neighborhood)); } @Test @DisplayName("사용자가 동네 경계 안에 있을 때 동네 인증 성공") public void testVerifyAndRegisterNeighborhood_WithinBoundary() { - given(distanceCalculator.calculateDistanceBetween(currentLocation, neighborhood.getLocation())) + given(distanceCalculator.calculateDistanceBetween(any(Location.class), any(Location.class))) .willReturn(5.0); - neighborhoodVerificationService.verifyAndRegisterNeighborhood(user, currentLocation, neighborhoodName); - + User user = new User(1L, "testUser"); + Location location = new Location(1L, 1L); + try (MockedStatic userMapperMock = + mockStatic(UserNeighborhoodVerifyRequestMapper.class); + MockedStatic locationMapperMock = + mockStatic(LocationNeighborhoodVerifyRequestMapper.class)) { + + userMapperMock.when(() -> + UserNeighborhoodVerifyRequestMapper.toDomain(userNeighborhoodVerifyRequest)) + .thenReturn(user); + + locationMapperMock.when(() -> + LocationNeighborhoodVerifyRequestMapper.toDomain(locationNeighborhoodVerifyRequest)) + .thenReturn(location); + + neighborhoodVerificationService.verifyAndRegisterNeighborhood( + userNeighborhoodVerifyRequest, + locationNeighborhoodVerifyRequest, + neighborhoodName); + } then(userRepository).should().save(user); } @Test @DisplayName("사용자가 동네 경계 밖에 있을 때 동네 인증 실패") public void testVerifyAndRegisterNeighborhood_OutsideBoundary() { - given(distanceCalculator.calculateDistanceBetween(currentLocation, neighborhood.getLocation())) + given(distanceCalculator.calculateDistanceBetween(any(Location.class), any(Location.class))) .willReturn(15.0); OutOfNeighborhoodBoundaryException exception = assertThrows(OutOfNeighborhoodBoundaryException.class, () -> { - neighborhoodVerificationService.verifyAndRegisterNeighborhood(user, currentLocation, neighborhoodName); + neighborhoodVerificationService.verifyAndRegisterNeighborhood(userNeighborhoodVerifyRequest, + locationNeighborhoodVerifyRequest, neighborhoodName); }); String expectedExceptionMessage = format("User is outside of the boundary of neighborhood: %s", neighborhoodName); assertThat(exception.getMessage(), is(expectedExceptionMessage)); - then(userRepository).should(never()).save(user); + then(userRepository).should(never()).save(any(User.class)); } } @@ -102,12 +124,13 @@ public void testVerifyAndRegisterNeighborhood_NeighborhoodNotFound() { NeighborhoodNotFoundException exception = assertThrows(NeighborhoodNotFoundException.class, () -> { - neighborhoodVerificationService.verifyAndRegisterNeighborhood(user, currentLocation, neighborhoodName); + neighborhoodVerificationService.verifyAndRegisterNeighborhood(userNeighborhoodVerifyRequest, + locationNeighborhoodVerifyRequest, neighborhoodName); }); String expectedExceptionMessage = format("Neighborhood not found: %s", neighborhoodName); assertThat(exception.getMessage(), is(expectedExceptionMessage)); - then(userRepository).should(never()).save(user); + then(userRepository).should(never()).save(any(User.class)); } } } \ No newline at end of file