-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #100 from f-lab-edu/feature-16-user-api-test
test:UserServiceTest.java 추가
- Loading branch information
Showing
4 changed files
with
266 additions
and
115 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
212 changes: 106 additions & 106 deletions
212
src/test/java/com/kboticket/service/ReservationServiceTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,106 +1,106 @@ | ||
package com.kboticket.service; | ||
|
||
import com.kboticket.enums.ErrorCode; | ||
import com.kboticket.exception.KboTicketException; | ||
import org.junit.Test; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.runner.RunWith; | ||
import org.mockito.InjectMocks; | ||
import org.mockito.Mock; | ||
import org.redisson.api.RLock; | ||
import org.redisson.api.RedissonClient; | ||
import org.springframework.boot.test.context.SpringBootTest; | ||
import org.springframework.test.context.junit4.SpringRunner; | ||
import org.springframework.transaction.annotation.Transactional; | ||
|
||
import java.util.Arrays; | ||
import java.util.HashSet; | ||
import java.util.Set; | ||
import java.util.concurrent.TimeUnit; | ||
|
||
import static org.junit.jupiter.api.Assertions.*; | ||
import static org.mockito.ArgumentMatchers.*; | ||
import static org.mockito.Mockito.*; | ||
|
||
@RunWith(SpringRunner.class) | ||
@SpringBootTest | ||
@Transactional | ||
public class ReservationServiceTest { | ||
|
||
@InjectMocks | ||
private ReservationService reservationService; | ||
|
||
@Mock | ||
private RedissonClient redissonClient; | ||
|
||
@Mock | ||
private RLock rLock; | ||
|
||
|
||
@BeforeEach | ||
void setUp() { | ||
when(redissonClient.getLock(anyString())).thenReturn(rLock); | ||
} | ||
|
||
@Test | ||
void shouldReserveSeatsSuccessfully() throws InterruptedException { | ||
Set<Long> seatIds = new HashSet<>(Arrays.asList(1L, 2L, 3L)); | ||
Long gameId = 1L; | ||
String email = "[email protected]"; | ||
|
||
when(rLock.tryLock(anyLong(), anyLong(), any(TimeUnit.class))).thenReturn(true); | ||
|
||
reservationService.reserve(seatIds, gameId, email); | ||
|
||
verify(rLock, times(seatIds.size())).tryLock(anyLong(), anyLong(), any(TimeUnit.class)); | ||
verify(rLock, times(0)).unlock(); | ||
} | ||
|
||
@Test | ||
void shouldThrowExceptionWhenSeatIsAlreadyHeld() throws Exception { | ||
Set<Long> seatIds = new HashSet<>(Arrays.asList(1L, 2L)); | ||
Long gameId = 1L; | ||
String email = "[email protected]"; | ||
|
||
when(rLock.tryLock(anyLong(), anyLong(), any(TimeUnit.class))).thenReturn(false); | ||
|
||
KboTicketException exception = assertThrows(KboTicketException.class, () -> { | ||
reservationService.reserve(seatIds, gameId, email); | ||
}); | ||
|
||
assertEquals("테스트가 실패하면 메시지 표", ErrorCode.FAILED_TRY_ROCK.message, exception.getMessage()); | ||
verify(rLock, times(1)).unlock(); | ||
} | ||
|
||
@Test | ||
void shouldReleaseLocksOnException() throws InterruptedException { | ||
Set<Long> seatIds = new HashSet<>(Arrays.asList(1L, 2L)); | ||
Long gameId = 1L; | ||
String email = "[email protected]"; | ||
|
||
when(rLock.tryLock(anyLong(), anyLong(), any(TimeUnit.class))) | ||
.thenReturn(true) | ||
.thenThrow(new KboTicketException(ErrorCode.FAILED_TRY_ROCK)); | ||
|
||
try { | ||
reservationService.reserve(seatIds, gameId, email); | ||
} catch (KboTicketException e) { | ||
// Exception is expected | ||
} | ||
|
||
verify(rLock, times(1)).unlock(); | ||
} | ||
|
||
@Test | ||
void shouldHandleInterruptedException() throws InterruptedException { | ||
Set<Long> seatIds = new HashSet<>(Arrays.asList(1L, 2L)); | ||
Long gameId = 1L; | ||
String email = "[email protected]"; | ||
|
||
when(rLock.tryLock(anyLong(), anyLong(), any(TimeUnit.class))).thenThrow(new InterruptedException()); | ||
|
||
assertDoesNotThrow(() -> reservationService.reserve(seatIds, gameId, email)); | ||
|
||
verify(rLock, times(1)).unlock(); | ||
} | ||
} | ||
//package com.kboticket.service; | ||
// | ||
//import com.kboticket.enums.ErrorCode; | ||
//import com.kboticket.exception.KboTicketException; | ||
//import org.junit.Test; | ||
//import org.junit.jupiter.api.BeforeEach; | ||
//import org.junit.runner.RunWith; | ||
//import org.mockito.InjectMocks; | ||
//import org.mockito.Mock; | ||
//import org.redisson.api.RLock; | ||
//import org.redisson.api.RedissonClient; | ||
//import org.springframework.boot.test.context.SpringBootTest; | ||
//import org.springframework.test.context.junit4.SpringRunner; | ||
//import org.springframework.transaction.annotation.Transactional; | ||
// | ||
//import java.util.Arrays; | ||
//import java.util.HashSet; | ||
//import java.util.Set; | ||
//import java.util.concurrent.TimeUnit; | ||
// | ||
//import static org.junit.jupiter.api.Assertions.*; | ||
//import static org.mockito.ArgumentMatchers.*; | ||
//import static org.mockito.Mockito.*; | ||
// | ||
//@RunWith(SpringRunner.class) | ||
//@SpringBootTest | ||
//@Transactional | ||
//public class ReservationServiceTest { | ||
// | ||
// @InjectMocks | ||
// private ReservationService reservationService; | ||
// | ||
// @Mock | ||
// private RedissonClient redissonClient; | ||
// | ||
// @Mock | ||
// private RLock rLock; | ||
// | ||
// | ||
// @BeforeEach | ||
// void setUp() { | ||
// when(redissonClient.getLock(anyString())).thenReturn(rLock); | ||
// } | ||
// | ||
// @Test | ||
// void shouldReserveSeatsSuccessfully() throws InterruptedException { | ||
// Set<Long> seatIds = new HashSet<>(Arrays.asList(1L, 2L, 3L)); | ||
// Long gameId = 1L; | ||
// String email = "[email protected]"; | ||
// | ||
// when(rLock.tryLock(anyLong(), anyLong(), any(TimeUnit.class))).thenReturn(true); | ||
// | ||
// reservationService.reserve(seatIds, gameId, email); | ||
// | ||
// verify(rLock, times(seatIds.size())).tryLock(anyLong(), anyLong(), any(TimeUnit.class)); | ||
// verify(rLock, times(0)).unlock(); | ||
// } | ||
// | ||
// @Test | ||
// void shouldThrowExceptionWhenSeatIsAlreadyHeld() throws Exception { | ||
// Set<Long> seatIds = new HashSet<>(Arrays.asList(1L, 2L)); | ||
// Long gameId = 1L; | ||
// String email = "[email protected]"; | ||
// | ||
// when(rLock.tryLock(anyLong(), anyLong(), any(TimeUnit.class))).thenReturn(false); | ||
// | ||
// KboTicketException exception = assertThrows(KboTicketException.class, () -> { | ||
// reservationService.reserve(seatIds, gameId, email); | ||
// }); | ||
// | ||
// assertEquals("테스트가 실패하면 메시지 표", ErrorCode.FAILED_TRY_ROCK.message, exception.getMessage()); | ||
// verify(rLock, times(1)).unlock(); | ||
// } | ||
// | ||
// @Test | ||
// void shouldReleaseLocksOnException() throws InterruptedException { | ||
// Set<Long> seatIds = new HashSet<>(Arrays.asList(1L, 2L)); | ||
// Long gameId = 1L; | ||
// String email = "[email protected]"; | ||
// | ||
// when(rLock.tryLock(anyLong(), anyLong(), any(TimeUnit.class))) | ||
// .thenReturn(true) | ||
// .thenThrow(new KboTicketException(ErrorCode.FAILED_TRY_ROCK)); | ||
// | ||
// try { | ||
// reservationService.reserve(seatIds, gameId, email); | ||
// } catch (KboTicketException e) { | ||
// // Exception is expected | ||
// } | ||
// | ||
// verify(rLock, times(1)).unlock(); | ||
// } | ||
// | ||
// @Test | ||
// void shouldHandleInterruptedException() throws InterruptedException { | ||
// Set<Long> seatIds = new HashSet<>(Arrays.asList(1L, 2L)); | ||
// Long gameId = 1L; | ||
// String email = "[email protected]"; | ||
// | ||
// when(rLock.tryLock(anyLong(), anyLong(), any(TimeUnit.class))).thenThrow(new InterruptedException()); | ||
// | ||
// assertDoesNotThrow(() -> reservationService.reserve(seatIds, gameId, email)); | ||
// | ||
// verify(rLock, times(1)).unlock(); | ||
// } | ||
//} |
Oops, something went wrong.