Skip to content

Commit

Permalink
test: 동네인증에서 DTO 사용에 따른 테스트 코드 수정
Browse files Browse the repository at this point in the history
  • Loading branch information
Srltas committed Sep 25, 2024
1 parent 31b6b94 commit e66d46e
Showing 1 changed file with 37 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand All @@ -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<UserNeighborhoodVerifyRequestMapper> userMapperMock =
mockStatic(UserNeighborhoodVerifyRequestMapper.class);
MockedStatic<LocationNeighborhoodVerifyRequestMapper> 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));
}
}

Expand All @@ -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));
}
}
}

0 comments on commit e66d46e

Please sign in to comment.