test: 提升ActivityController测试覆盖率 - 新增topN边界测试

- 新增3个测试覆盖topN参数的边界条件
  * topN超过列表大小的场景
  * topN为0的场景
  * topN为负数的场景

覆盖率提升:
- 总体分支覆盖率: 63.3% → 63.6% (+2个分支)
- Controller包: 84% → 89% (+5%)
- ActivityController: 77% → 更高
- 新增测试用例: 3个
- 距离70%目标: 还需40个分支

本次会话总成果:
- 新增测试: 12个
- 分支覆盖: +7个 (404→411)
- Controller包: 73% → 89% (+16%)
This commit is contained in:
Your Name
2026-03-03 12:03:56 +08:00
parent 81934725f3
commit bbd27dca1d
2 changed files with 54 additions and 1 deletions

View File

@@ -282,4 +282,54 @@ class ActivityControllerContractTest {
.andExpect(status().isOk())
.andExpect(header().string("Content-Type", "text/csv;charset=UTF-8"));
}
@Test
void shouldGetLeaderboard_whenTopNExceedsListSize() throws Exception {
List<LeaderboardEntry> entries = new ArrayList<>();
for (int i = 1; i <= 5; i++) {
entries.add(new LeaderboardEntry((long) i, "用户" + i, 100 - i));
}
when(activityService.getLeaderboard(1L)).thenReturn(entries);
mockMvc.perform(get("/api/v1/activities/1/leaderboard")
.param("topN", "100")
.accept(MediaType.APPLICATION_JSON)
.header("X-API-Key", TestAuthSupport.RAW_API_KEY)
.header("Authorization", "Bearer test-token"))
.andExpect(status().isOk())
.andExpect(jsonPath("$.code").value(200))
.andExpect(jsonPath("$.data.length()").value(5));
}
@Test
void shouldGetLeaderboard_whenTopNIsZero() throws Exception {
List<LeaderboardEntry> entries = new ArrayList<>();
entries.add(new LeaderboardEntry(1L, "用户1", 100));
when(activityService.getLeaderboard(1L)).thenReturn(entries);
mockMvc.perform(get("/api/v1/activities/1/leaderboard")
.param("topN", "0")
.accept(MediaType.APPLICATION_JSON)
.header("X-API-Key", TestAuthSupport.RAW_API_KEY)
.header("Authorization", "Bearer test-token"))
.andExpect(status().isOk())
.andExpect(jsonPath("$.code").value(200))
.andExpect(jsonPath("$.data.length()").value(1));
}
@Test
void shouldGetLeaderboard_whenTopNIsNegative() throws Exception {
List<LeaderboardEntry> entries = new ArrayList<>();
entries.add(new LeaderboardEntry(1L, "用户1", 100));
when(activityService.getLeaderboard(1L)).thenReturn(entries);
mockMvc.perform(get("/api/v1/activities/1/leaderboard")
.param("topN", "-1")
.accept(MediaType.APPLICATION_JSON)
.header("X-API-Key", TestAuthSupport.RAW_API_KEY)
.header("Authorization", "Bearer test-token"))
.andExpect(status().isOk())
.andExpect(jsonPath("$.code").value(200))
.andExpect(jsonPath("$.data.length()").value(1));
}
}