Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[#16] 동네 인증 테스트 코드 리펙토링 #19

Merged
merged 1 commit into from
Sep 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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));
}
}
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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
Expand All @@ -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);
}
}
}
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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);
}
}
Loading