Skip to content

Commit

Permalink
test: 동네 인증 유즈케이스 테스트 코드 수정
Browse files Browse the repository at this point in the history
Nested 애노테이션을 이용해 테스트 목적에 따라 분리
  • Loading branch information
Srltas committed Sep 18, 2024
1 parent b66f2c7 commit e24cc00
Showing 1 changed file with 40 additions and 25 deletions.
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package com.srltas.runtogether.application;

import static java.lang.String.format;
import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.Mockito.*;
import static org.mockito.MockitoAnnotations.*;

import java.util.Optional;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import org.mockito.InjectMocks;
import org.mockito.Mock;
Expand Down Expand Up @@ -44,39 +46,52 @@ public void setUp() {
openMocks(this);
}

@Test
public void testVerifyAndRegisterNeighborhood_WithinBoundary() {
when(neighborhoodRepository.findByName(neighborhoodName)).thenReturn(Optional.of(neighborhood));
when(distanceCalculator.calculateDistanceBetween(currentLocation, neighborhood.getLocation())).thenReturn(5.0);
@Nested
class WhenNeighborhoodIsFound {

neighborhoodVerificationService.verifyAndRegisterNeighborhood(user, currentLocation, neighborhoodName);
@BeforeEach
public void setUp() {
when(neighborhoodRepository.findByName(neighborhoodName)).thenReturn(Optional.of(neighborhood));
}

verify(verifiedNeighborhoodRepository).saveVerifiedNeighborhood(user, neighborhood);
}

@Test
public void testVerifyAndRegisterNeighborhood_OutsideBoundary() {
when(neighborhoodRepository.findByName(neighborhoodName)).thenReturn(Optional.of(neighborhood));
when(distanceCalculator.calculateDistanceBetween(currentLocation, neighborhood.getLocation())).thenReturn(15.0);
@Test
public void testVerifyAndRegisterNeighborhood_WithinBoundary() {
when(distanceCalculator.calculateDistanceBetween(currentLocation, neighborhood.getLocation()))
.thenReturn(5.0);

OutOfNeighborhoodBoundaryException exception = assertThrows(OutOfNeighborhoodBoundaryException.class, () -> {
neighborhoodVerificationService.verifyAndRegisterNeighborhood(user, currentLocation, neighborhoodName);
});

assertEquals(String.format("User is outside of the boundary of neighborhood: %s", neighborhoodName),
exception.getMessage());
verify(verifiedNeighborhoodRepository, never()).saveVerifiedNeighborhood(user, neighborhood);
verify(verifiedNeighborhoodRepository).saveVerifiedNeighborhood(user, neighborhood);
}

@Test
public void testVerifyAndRegisterNeighborhood_OutsideBoundary() {
when(distanceCalculator.calculateDistanceBetween(currentLocation, neighborhood.getLocation()))
.thenReturn(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(verifiedNeighborhoodRepository, never()).saveVerifiedNeighborhood(user, neighborhood);
}
}

@Test
public void testVerifyAndRegisterNeighborhood_NeighborhoodNotFound() {
when(neighborhoodRepository.findByName("Homaesil")).thenReturn(Optional.empty());
@Nested
class WhenNeighborhoodIsNotFound {
@Test
public void testVerifyAndRegisterNeighborhood_NeighborhoodNotFound() {
when(neighborhoodRepository.findByName("Homaesil")).thenReturn(Optional.empty());

NeighborhoodNotFoundException exception = assertThrows(NeighborhoodNotFoundException.class, () -> {
neighborhoodVerificationService.verifyAndRegisterNeighborhood(user, currentLocation, neighborhoodName);
});
NeighborhoodNotFoundException exception = assertThrows(NeighborhoodNotFoundException.class,
() -> {
neighborhoodVerificationService.verifyAndRegisterNeighborhood(user, currentLocation, neighborhoodName);
});

assertEquals(String.format("Neighborhood not found: %s", neighborhoodName), exception.getMessage());
verify(verifiedNeighborhoodRepository, never()).saveVerifiedNeighborhood(user, neighborhood);
assertEquals(format("Neighborhood not found: %s", neighborhoodName), exception.getMessage());
verify(verifiedNeighborhoodRepository, never()).saveVerifiedNeighborhood(user, neighborhood);
}
}
}

0 comments on commit e24cc00

Please sign in to comment.