Skip to content

Commit 205e26f

Browse files
committed
test: RankingSchedulerTest 클래스 추가 및 랭킹 스케줄러 기능 테스트 구현
1 parent 43d49a1 commit 205e26f

1 file changed

Lines changed: 71 additions & 0 deletions

File tree

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package com.loopers.domain.ranking;
2+
3+
import com.loopers.application.ranking.RankingScheduler;
4+
import com.loopers.domain.ranking.RankingService;
5+
import org.junit.jupiter.api.DisplayName;
6+
import org.junit.jupiter.api.Test;
7+
import org.junit.jupiter.api.extension.ExtendWith;
8+
import org.mockito.InjectMocks;
9+
import org.mockito.Mock;
10+
import org.mockito.junit.jupiter.MockitoExtension;
11+
import org.springframework.test.util.ReflectionTestUtils;
12+
13+
import java.time.*;
14+
15+
import static org.mockito.Mockito.*;
16+
17+
@ExtendWith(MockitoExtension.class)
18+
class RankingSchedulerTest {
19+
20+
@Mock
21+
private RankingService rankingService;
22+
23+
@InjectMocks
24+
private RankingScheduler rankingScheduler;
25+
26+
@Test
27+
@DisplayName("prepareNextDayRanking 실행 시 오늘 → 내일로 carryOver가 호출된다")
28+
void shouldCallCarryOverWithTodayAndTomorrow() {
29+
// given
30+
LocalDate fixedDate = LocalDate.of(2025, 1, 15);
31+
Clock fixedClock = Clock.fixed(
32+
fixedDate.atTime(23, 50).toInstant(ZoneOffset.of("+09:00")),
33+
ZoneId.of("Asia/Seoul")
34+
);
35+
36+
ReflectionTestUtils.setField(rankingScheduler, "clock", fixedClock);
37+
ReflectionTestUtils.setField(rankingScheduler, "carryOverWeight", 0.1);
38+
39+
// when
40+
rankingScheduler.prepareNextDayRanking();
41+
42+
// then
43+
verify(rankingService).carryOverScores(
44+
LocalDate.of(2025, 1, 15),
45+
LocalDate.of(2025, 1, 16),
46+
0.1
47+
);
48+
}
49+
50+
@Test
51+
@DisplayName("carryOverScores 예외 발생 시에도 정상 종료된다")
52+
void shouldHandleExceptionGracefully() {
53+
// given
54+
LocalDate fixedDate = LocalDate.of(2025, 1, 15);
55+
Clock fixedClock = Clock.fixed(
56+
fixedDate.atTime(23, 50).toInstant(ZoneOffset.of("+09:00")),
57+
ZoneId.of("Asia/Seoul")
58+
);
59+
60+
ReflectionTestUtils.setField(rankingScheduler, "clock", fixedClock);
61+
ReflectionTestUtils.setField(rankingScheduler, "carryOverWeight", 0.1);
62+
63+
doThrow(new RuntimeException("Redis error"))
64+
.when(rankingService).carryOverScores(any(), any(), anyDouble());
65+
66+
// when & then (예외 전파 없이 종료)
67+
rankingScheduler.prepareNextDayRanking();
68+
69+
verify(rankingService).carryOverScores(any(), any(), anyDouble());
70+
}
71+
}

0 commit comments

Comments
 (0)