diff --git a/src/test/java/com/srltas/runtogether/adapter/out/HaversineDistanceCalculatorTest.java b/src/test/java/com/srltas/runtogether/adapter/out/HaversineDistanceCalculatorTest.java index e1abfe1..82e0608 100644 --- a/src/test/java/com/srltas/runtogether/adapter/out/HaversineDistanceCalculatorTest.java +++ b/src/test/java/com/srltas/runtogether/adapter/out/HaversineDistanceCalculatorTest.java @@ -1,7 +1,9 @@ package com.srltas.runtogether.adapter.out; -import static org.junit.jupiter.api.Assertions.*; +import static org.hamcrest.MatcherAssert.*; +import static org.hamcrest.Matchers.*; +import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; import com.srltas.runtogether.domain.model.location.Location; @@ -14,18 +16,20 @@ class HaversineDistanceCalculatorTest { private final Location busan = new Location(35.179554, 129.075641); @Test + @DisplayName("서로 다른 두 위치 간의 거리를 계산") public void testCalculateDistanceBetween_TwoLocations() { double seoulBusanDistance = 325.0; double distance = haversineDistanceCalculator.calculateDistanceBetween(seoul, busan); - assertEquals(seoulBusanDistance, distance, 1.0); + assertThat(distance, is(closeTo(seoulBusanDistance, 1.0))); } @Test + @DisplayName("같은 위치 간의 거리를 계산") public void testCalculateDistanceBetween_SameLocation() { double distance = haversineDistanceCalculator.calculateDistanceBetween(seoul, seoul); - assertEquals(0.0, distance); + assertThat(distance, is(0.0)); } } \ No newline at end of file diff --git a/src/test/java/com/srltas/runtogether/application/NeighborhoodVerificationServiceTest.java b/src/test/java/com/srltas/runtogether/application/NeighborhoodVerificationServiceTest.java index a5ffc55..0d7c2b3 100644 --- a/src/test/java/com/srltas/runtogether/application/NeighborhoodVerificationServiceTest.java +++ b/src/test/java/com/srltas/runtogether/application/NeighborhoodVerificationServiceTest.java @@ -1,17 +1,22 @@ package com.srltas.runtogether.application; -import static java.lang.String.format; +import static java.lang.String.*; +import static org.hamcrest.MatcherAssert.*; +import static org.hamcrest.Matchers.*; import static org.junit.jupiter.api.Assertions.*; -import static org.mockito.Mockito.*; -import static org.mockito.MockitoAnnotations.*; +import static org.mockito.BDDMockito.any; +import static org.mockito.BDDMockito.*; import java.util.Optional; 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.junit.jupiter.api.extension.ExtendWith; import org.mockito.InjectMocks; import org.mockito.Mock; +import org.mockito.junit.jupiter.MockitoExtension; import com.srltas.runtogether.domain.exception.NeighborhoodNotFoundException; import com.srltas.runtogether.domain.exception.OutOfNeighborhoodBoundaryException; @@ -22,6 +27,7 @@ import com.srltas.runtogether.domain.model.user.User; import com.srltas.runtogether.domain.model.user.UserRepository; +@ExtendWith(MockitoExtension.class) class NeighborhoodVerificationServiceTest { @Mock @@ -36,63 +42,72 @@ class NeighborhoodVerificationServiceTest { @InjectMocks private NeighborhoodVerificationService neighborhoodVerificationService; - private User user = new User(1L, "testUser"); - private String neighborhoodName = "Gangnam"; - private Location currentLocation = new Location(37.505858, 127.058319); + private User user; + private String neighborhoodName; + private Location currentLocation; private Neighborhood neighborhood; @BeforeEach public void setUp() { - openMocks(this); + 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); } @Nested + @DisplayName("동네가 존재하는 경우") class WhenNeighborhoodIsFound { @BeforeEach public void setUp() { - when(neighborhoodRepository.findByName(neighborhoodName)).thenReturn(Optional.of(neighborhood)); + given(neighborhoodRepository.findByName(neighborhoodName)).willReturn(Optional.of(neighborhood)); } @Test + @DisplayName("사용자가 동네 경계 안에 있을 때 동네 인증 성공") public void testVerifyAndRegisterNeighborhood_WithinBoundary() { - when(distanceCalculator.calculateDistanceBetween(currentLocation, neighborhood.getLocation())) - .thenReturn(5.0); + given(distanceCalculator.calculateDistanceBetween(currentLocation, neighborhood.getLocation())) + .willReturn(5.0); neighborhoodVerificationService.verifyAndRegisterNeighborhood(user, currentLocation, neighborhoodName); - verify(userRepository).save(user); + then(userRepository).should().save(user); } @Test + @DisplayName("사용자가 동네 경계 밖에 있을 때 동네 인증 실패") public void testVerifyAndRegisterNeighborhood_OutsideBoundary() { - when(distanceCalculator.calculateDistanceBetween(currentLocation, neighborhood.getLocation())) - .thenReturn(15.0); + given(distanceCalculator.calculateDistanceBetween(currentLocation, neighborhood.getLocation())) + .willReturn(15.0); OutOfNeighborhoodBoundaryException exception = assertThrows(OutOfNeighborhoodBoundaryException.class, () -> { neighborhoodVerificationService.verifyAndRegisterNeighborhood(user, currentLocation, neighborhoodName); }); - assertEquals(format("User is outside of the boundary of neighborhood: %s", neighborhoodName), exception.getMessage()); - verify(userRepository, never()).save(user); + String expectedExceptionMessage = format("User is outside of the boundary of neighborhood: %s", neighborhoodName); + assertThat(exception.getMessage(), is(expectedExceptionMessage)); + then(userRepository).should(never()).save(user); } } @Nested + @DisplayName("동네가 존재하지 않는 경우") class WhenNeighborhoodIsNotFound { @Test + @DisplayName("존재하지 않는 동네를 찾아 예외 발생") public void testVerifyAndRegisterNeighborhood_NeighborhoodNotFound() { - when(neighborhoodRepository.findByName("Homaesil")).thenReturn(Optional.empty()); + given(neighborhoodRepository.findByName(any())).willReturn(Optional.empty()); NeighborhoodNotFoundException exception = assertThrows(NeighborhoodNotFoundException.class, () -> { neighborhoodVerificationService.verifyAndRegisterNeighborhood(user, currentLocation, neighborhoodName); }); - assertEquals(format("Neighborhood not found: %s", neighborhoodName), exception.getMessage()); - verify(userRepository, never()).save(user); + String expectedExceptionMessage = format("Neighborhood not found: %s", neighborhoodName); + assertThat(exception.getMessage(), is(expectedExceptionMessage)); + then(userRepository).should(never()).save(user); } } } \ No newline at end of file diff --git a/src/test/java/com/srltas/runtogether/domain/model/neighborhood/NeighborhoodTest.java b/src/test/java/com/srltas/runtogether/domain/model/neighborhood/NeighborhoodTest.java index 7675c27..d605b99 100644 --- a/src/test/java/com/srltas/runtogether/domain/model/neighborhood/NeighborhoodTest.java +++ b/src/test/java/com/srltas/runtogether/domain/model/neighborhood/NeighborhoodTest.java @@ -1,16 +1,20 @@ package com.srltas.runtogether.domain.model.neighborhood; -import static org.junit.jupiter.api.Assertions.*; -import static org.mockito.Mockito.*; -import static org.mockito.MockitoAnnotations.*; +import static org.hamcrest.MatcherAssert.*; +import static org.hamcrest.Matchers.*; +import static org.mockito.BDDMockito.*; import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; import org.mockito.Mock; +import org.mockito.junit.jupiter.MockitoExtension; import com.srltas.runtogether.domain.model.location.DistanceCalculator; import com.srltas.runtogether.domain.model.location.Location; +@ExtendWith(MockitoExtension.class) class NeighborhoodTest { @Mock @@ -22,30 +26,30 @@ class NeighborhoodTest { @BeforeEach public void setUp() { - openMocks(this); - currentLocation = new Location(37.505858, 127.058319); neighborhoodLocation = new Location(37.517347, 127.047382); neighborhood = new Neighborhood("Gangnam", neighborhoodLocation, 7.0, distanceCalculator); } @Test + @DisplayName("사용자와 동네 기준이 설정된 범위보다 작을 때, 동네 내에 있다고 판단") public void testIsWithinBoundary_WhenUserInSideBoundary() { - when(distanceCalculator.calculateDistanceBetween(currentLocation, neighborhoodLocation)).thenReturn(5.0); + given(distanceCalculator.calculateDistanceBetween(currentLocation, neighborhoodLocation)).willReturn(5.0); boolean isInSide = neighborhood.isWithinBoundary(currentLocation); - assertTrue(isInSide); - verify(distanceCalculator).calculateDistanceBetween(currentLocation, neighborhoodLocation); + assertThat(isInSide, is(true)); + then(distanceCalculator).should().calculateDistanceBetween(currentLocation, neighborhoodLocation); } @Test + @DisplayName("사용자와 동네 기준이 설정된 범위보다 클 때, 동네 밖에 있다고 판단") public void testIsWithinBoundary_WhenUserOutsideBoundary() { when(distanceCalculator.calculateDistanceBetween(currentLocation, neighborhoodLocation)).thenReturn(10.0); boolean isInSide = neighborhood.isWithinBoundary(currentLocation); - assertFalse(isInSide); - verify(distanceCalculator).calculateDistanceBetween(currentLocation, neighborhoodLocation); + assertThat(isInSide, is(false)); + then(distanceCalculator).should().calculateDistanceBetween(currentLocation, neighborhoodLocation); } } \ No newline at end of file