فهرست منبع

test(S-cfc-4): 补齐 guide.trainingPlan.getPlanList 契约级单测

能力键 guide.trainingPlan.getPlanList(POST /api/guide/training-plans/list)
原为未覆盖 handler,本提交补 9 条契约级单测(MockMvc + 直接调用级):
- 正常路径-无 memberId:走 getPlansByGuide(guideId),code 200 + data
- 正常路径-带 body.memberId:走 getPlansByGuideAndChild(guideId, memberId)
- 边界-无 body.memberId 回退 currentMemberId
- 边界-body.memberId 优先于 currentMemberId
- 边界-非数字/空字符串 memberId 经 ParamUtils 回退 currentMemberId
- 边界-Service 返回空列表:code 200 + data=[]
- 直接调用级:Service 返回的列表原样透传(assertSame)
角色/权限口径:本 handler 无 @RequestAttribute(role/roles),授权由 JwtInterceptor
(匿名注入 userId=-1 被 401 拦截)承担,故覆盖 guideId 传递正确性而非角色分支。
不改 src/main 实现;测试 mock TrainingPlanService,不依赖真实微信/外部服务。

9 用例全部通过(mvn -DskipTests=false -Dtest=TrainingPlanControllerGetPlanListTest test)。
Sisyphus 2 هفته پیش
والد
کامیت
26d636dbe0

+ 237 - 0
cfc-backend/src/test/java/com/etotem/cfc/controller/guide/TrainingPlanControllerGetPlanListTest.java

@@ -0,0 +1,237 @@
+package com.etotem.cfc.controller.guide;
+
+import com.etotem.cfc.common.Result;
+import com.etotem.cfc.entity.TrainingPlan;
+import com.etotem.cfc.service.TrainingPlanService;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import org.mockito.ArgumentCaptor;
+import org.mockito.Mock;
+import org.mockito.MockitoAnnotations;
+import org.springframework.http.MediaType;
+import org.springframework.test.util.ReflectionTestUtils;
+import org.springframework.test.web.servlet.MockMvc;
+import org.springframework.test.web.servlet.setup.MockMvcBuilders;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertSame;
+import static org.mockito.ArgumentMatchers.anyLong;
+import static org.mockito.ArgumentMatchers.eq;
+import static org.mockito.Mockito.*;
+import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
+import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
+import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
+
+/**
+ * TDD 覆盖驱动(S-cfc-4 / 能力键 guide.trainingPlan.getPlanList):
+ * POST /api/guide/training-plans/list 获取规划师训练方案列表 的契约级单测。
+ *
+ * 契约(对应 TrainingPlanController.getPlanList):
+ * - memberId 解析顺序:body.memberId(ParamUtils.getLong)优先,缺失/非法则回退 currentMemberId
+ * - memberId != null → trainingPlanService.getPlansByGuideAndChild(guideId, memberId)
+ * - memberId == null → trainingPlanService.getPlansByGuide(guideId)
+ * - 返回统一 Result.success(plans),code=200,data=服务返回列表
+ *
+ * 角色/权限口径:getPlanList 处理器无 @RequestAttribute("role"/"roles"),授权交由 JwtInterceptor
+ * (匿名请求注入 userId=-1 被 401 拦截)承担;本控制器唯一身份边界是 @RequestAttribute("userId")
+ * required=true(guideId=登录账号),本测试覆盖该 guideId 传递正确性而非角色分支。
+ * 测试不依赖真实微信/外部服务:TrainingPlanService 全 mock。
+ */
+public class TrainingPlanControllerGetPlanListTest {
+
+    @Mock private TrainingPlanService trainingPlanService;
+
+    private MockMvc mockMvc;
+    private TrainingPlanController controller;
+
+    @BeforeEach
+    public void setUp() {
+        MockitoAnnotations.openMocks(this);
+        controller = new TrainingPlanController();
+        ReflectionTestUtils.setField(controller, "trainingPlanService", trainingPlanService);
+        mockMvc = MockMvcBuilders.standaloneSetup(controller).build();
+    }
+
+    private TrainingPlan plan(long id, String title) {
+        TrainingPlan p = new TrainingPlan();
+        p.setId(id);
+        p.setTitle(title);
+        return p;
+    }
+
+    @Test
+    public void getPlanList_withoutMemberId_usesGetPlansByGuide() throws Exception {
+        Long guideId = 9L;
+        List<TrainingPlan> plans = Arrays.asList(plan(1L, "方案A"), plan(2L, "方案B"));
+        when(trainingPlanService.getPlansByGuide(guideId)).thenReturn(plans);
+
+        mockMvc.perform(post("/api/guide/training-plans/list")
+                        .contentType(MediaType.APPLICATION_JSON)
+                        .content("{}")
+                        .requestAttr("userId", guideId))
+                .andExpect(status().isOk())
+                .andExpect(jsonPath("$.code").value(200))
+                .andExpect(jsonPath("$.data[0].id").value(1))
+                .andExpect(jsonPath("$.data[0].title").value("方案A"))
+                .andExpect(jsonPath("$.data[1].id").value(2));
+
+        verify(trainingPlanService).getPlansByGuide(guideId);
+        verify(trainingPlanService, never()).getPlansByGuideAndChild(anyLong(), anyLong());
+    }
+
+    @Test
+    public void getPlanList_withBodyMemberId_usesGetPlansByGuideAndChild() throws Exception {
+        Long guideId = 9L;
+        Long memberId = 55L;
+        List<TrainingPlan> plans = Collections.singletonList(plan(3L, "针对成员"));
+        when(trainingPlanService.getPlansByGuideAndChild(guideId, memberId)).thenReturn(plans);
+
+        mockMvc.perform(post("/api/guide/training-plans/list")
+                        .contentType(MediaType.APPLICATION_JSON)
+                        .content("{\"memberId\":55}")
+                        .requestAttr("userId", guideId))
+                .andExpect(status().isOk())
+                .andExpect(jsonPath("$.code").value(200))
+                .andExpect(jsonPath("$.data[0].id").value(3));
+
+        verify(trainingPlanService).getPlansByGuideAndChild(guideId, memberId);
+        verify(trainingPlanService, never()).getPlansByGuide(anyLong());
+    }
+
+    @Test
+    public void getPlanList_withoutBodyMemberId_fallsBackToCurrentMemberId() throws Exception {
+        Long guideId = 9L;
+        Long currentMemberId = 42L;
+        List<TrainingPlan> plans = Collections.singletonList(plan(4L, "回退成员"));
+        when(trainingPlanService.getPlansByGuideAndChild(guideId, currentMemberId)).thenReturn(plans);
+
+        mockMvc.perform(post("/api/guide/training-plans/list")
+                        .contentType(MediaType.APPLICATION_JSON)
+                        .content("{}")
+                        .requestAttr("userId", guideId)
+                        .requestAttr("currentMemberId", currentMemberId))
+                .andExpect(status().isOk())
+                .andExpect(jsonPath("$.code").value(200))
+                .andExpect(jsonPath("$.data[0].id").value(4));
+
+        verify(trainingPlanService).getPlansByGuideAndChild(guideId, currentMemberId);
+        verify(trainingPlanService, never()).getPlansByGuide(anyLong());
+    }
+
+    @Test
+    public void getPlanList_bodyMemberIdTakesPrecedenceOverCurrentMemberId() throws Exception {
+        Long guideId = 9L;
+        Long bodyMemberId = 7L;
+        Long currentMemberId = 42L;
+        List<TrainingPlan> plans = Collections.singletonList(plan(5L, "body优先"));
+        when(trainingPlanService.getPlansByGuideAndChild(guideId, bodyMemberId)).thenReturn(plans);
+
+        mockMvc.perform(post("/api/guide/training-plans/list")
+                        .contentType(MediaType.APPLICATION_JSON)
+                        .content("{\"memberId\":7}")
+                        .requestAttr("userId", guideId)
+                        .requestAttr("currentMemberId", currentMemberId))
+                .andExpect(status().isOk())
+                .andExpect(jsonPath("$.code").value(200))
+                .andExpect(jsonPath("$.data[0].id").value(5));
+
+        verify(trainingPlanService).getPlansByGuideAndChild(guideId, bodyMemberId);
+        verify(trainingPlanService, never()).getPlansByGuide(anyLong());
+    }
+
+    @Test
+    public void getPlanList_nonNumericBodyMemberId_fallsBackToCurrentMemberId() throws Exception {
+        Long guideId = 9L;
+        Long currentMemberId = 42L;
+        List<TrainingPlan> plans = Collections.singletonList(plan(6L, "非法回退"));
+        when(trainingPlanService.getPlansByGuideAndChild(guideId, currentMemberId)).thenReturn(plans);
+
+        // ParamUtils.getLong 对非数字返回 null,应回退 currentMemberId
+        mockMvc.perform(post("/api/guide/training-plans/list")
+                        .contentType(MediaType.APPLICATION_JSON)
+                        .content("{\"memberId\":\"abc\"}")
+                        .requestAttr("userId", guideId)
+                        .requestAttr("currentMemberId", currentMemberId))
+                .andExpect(status().isOk())
+                .andExpect(jsonPath("$.code").value(200))
+                .andExpect(jsonPath("$.data[0].id").value(6));
+
+        verify(trainingPlanService).getPlansByGuideAndChild(guideId, currentMemberId);
+        verify(trainingPlanService, never()).getPlansByGuide(anyLong());
+    }
+
+    @Test
+    public void getPlanList_emptyBodyMemberId_fallsBackToCurrentMemberId() throws Exception {
+        Long guideId = 9L;
+        Long currentMemberId = 42L;
+        List<TrainingPlan> plans = Collections.singletonList(plan(7L, "空串回退"));
+        when(trainingPlanService.getPlansByGuideAndChild(guideId, currentMemberId)).thenReturn(plans);
+
+        // ParamUtils.getLong 对空字符串返回 null,应回退 currentMemberId
+        mockMvc.perform(post("/api/guide/training-plans/list")
+                        .contentType(MediaType.APPLICATION_JSON)
+                        .content("{\"memberId\":\"\"}")
+                        .requestAttr("userId", guideId)
+                        .requestAttr("currentMemberId", currentMemberId))
+                .andExpect(status().isOk())
+                .andExpect(jsonPath("$.code").value(200))
+                .andExpect(jsonPath("$.data[0].id").value(7));
+
+        verify(trainingPlanService).getPlansByGuideAndChild(guideId, currentMemberId);
+        verify(trainingPlanService, never()).getPlansByGuide(anyLong());
+    }
+
+    @Test
+    public void getPlanList_serviceReturnsEmptyList_returns200EmptyData() throws Exception {
+        Long guideId = 9L;
+        when(trainingPlanService.getPlansByGuide(guideId)).thenReturn(Collections.emptyList());
+
+        mockMvc.perform(post("/api/guide/training-plans/list")
+                        .contentType(MediaType.APPLICATION_JSON)
+                        .content("{}")
+                        .requestAttr("userId", guideId))
+                .andExpect(status().isOk())
+                .andExpect(jsonPath("$.code").value(200))
+                .andExpect(jsonPath("$.data").isArray())
+                .andExpect(jsonPath("$.data.length()").value(0));
+    }
+
+    @Test
+    public void getPlanList_directInvocation_propagatesServiceResult() {
+        Long guideId = 9L;
+        List<TrainingPlan> plans = new ArrayList<>();
+        plans.add(plan(11L, "直接调用"));
+        when(trainingPlanService.getPlansByGuide(guideId)).thenReturn(plans);
+
+        Map<String, Object> body = new HashMap<>();
+        Result<List<TrainingPlan>> result = controller.getPlanList(guideId, body, null);
+
+        assertEquals(Integer.valueOf(200), result.getCode());
+        assertSame(plans, result.getData());
+        verify(trainingPlanService).getPlansByGuide(guideId);
+    }
+
+    @Test
+    public void getPlanList_directInvocation_withMemberId_passesGuideIdAndMemberId() {
+        Long guideId = 9L;
+        Long memberId = 88L;
+        List<TrainingPlan> plans = Collections.singletonList(plan(12L, "成员"));
+        when(trainingPlanService.getPlansByGuideAndChild(guideId, memberId)).thenReturn(plans);
+
+        Map<String, Object> body = new HashMap<>();
+        body.put("memberId", memberId);
+        Result<List<TrainingPlan>> result = controller.getPlanList(guideId, body, null);
+
+        assertEquals(Integer.valueOf(200), result.getCode());
+        assertSame(plans, result.getData());
+        verify(trainingPlanService).getPlansByGuideAndChild(guideId, memberId);
+        verify(trainingPlanService, never()).getPlansByGuide(anyLong());
+    }
+}