|
@@ -0,0 +1,90 @@
|
|
|
|
|
+package com.etotem.cfc.controller;
|
|
|
|
|
+
|
|
|
|
|
+import com.etotem.cfc.entity.AssessmentMaterial;
|
|
|
|
|
+import com.etotem.cfc.service.AssessmentService;
|
|
|
|
|
+import org.junit.jupiter.api.BeforeEach;
|
|
|
|
|
+import org.junit.jupiter.api.Test;
|
|
|
|
|
+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 static org.mockito.Mockito.when;
|
|
|
|
|
+import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
|
|
|
|
|
+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;
|
|
|
|
|
+
|
|
|
|
|
+/**
|
|
|
|
|
+ * 契约级单测:{@link DanAssessmentController#getActiveMaterial}(能力键 dan.assessment.getActiveMaterial)。
|
|
|
|
|
+ *
|
|
|
|
|
+ * 端点:POST /api/dan-assessment/material/active
|
|
|
|
|
+ *
|
|
|
|
|
+ * 契约要点(按实体 + 控制器 + AGENTS.md):
|
|
|
|
|
+ * - 统一 @PostMapping(GET 应 405);
|
|
|
|
|
+ * - 统一 Result<T> 包装(code=200 / message=success / data=当前启用材料);
|
|
|
|
|
+ * - AssessmentMaterial 实体字段为 title(无 name),data 序列化含 id/title/status。
|
|
|
|
|
+ *
|
|
|
|
|
+ * 测试隔离:MockMvc standalone + mock AssessmentService,无 Spring 上下文 / 无 DB。
|
|
|
|
|
+ * 注意:实体字段是 {@code title} 而非 {@code name}——历史编译阻断即因误用 setName 触发,
|
|
|
|
|
+ * 本测试以 setTitle 为准(对齐 AssessmentMaterial.java 真实字段)。
|
|
|
|
|
+ */
|
|
|
|
|
+class DanAssessmentControllerGetActiveMaterialTest {
|
|
|
|
|
+
|
|
|
|
|
+ private static final String URL = "/api/dan-assessment/material/active";
|
|
|
|
|
+
|
|
|
|
|
+ @Mock
|
|
|
|
|
+ private AssessmentService assessmentService;
|
|
|
|
|
+
|
|
|
|
|
+ private MockMvc mockMvc;
|
|
|
|
|
+
|
|
|
|
|
+ @BeforeEach
|
|
|
|
|
+ void setUp() {
|
|
|
|
|
+ MockitoAnnotations.openMocks(this);
|
|
|
|
|
+ DanAssessmentController controller = new DanAssessmentController();
|
|
|
|
|
+ ReflectionTestUtils.setField(controller, "assessmentService", assessmentService);
|
|
|
|
|
+ mockMvc = MockMvcBuilders.standaloneSetup(controller).build();
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private AssessmentMaterial material(long id, String title, String status) {
|
|
|
|
|
+ AssessmentMaterial m = new AssessmentMaterial();
|
|
|
|
|
+ m.setId(id);
|
|
|
|
|
+ m.setTitle(title); // 实体字段为 title(非 name),与 AssessmentMaterial.java 一致
|
|
|
|
|
+ m.setStatus(status);
|
|
|
|
|
+ return m;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Test
|
|
|
|
|
+ void getActiveMaterial_返回当前启用材料() throws Exception {
|
|
|
|
|
+ AssessmentMaterial active = material(1L, "2026 春季测评", "active");
|
|
|
|
|
+ when(assessmentService.getActiveMaterial()).thenReturn(active);
|
|
|
|
|
+
|
|
|
|
|
+ mockMvc.perform(post(URL)
|
|
|
|
|
+ .contentType(MediaType.APPLICATION_JSON))
|
|
|
|
|
+ .andExpect(status().isOk())
|
|
|
|
|
+ .andExpect(jsonPath("$.code").value(200))
|
|
|
|
|
+ .andExpect(jsonPath("$.message").value("success"))
|
|
|
|
|
+ .andExpect(jsonPath("$.data.id").value(1))
|
|
|
|
|
+ .andExpect(jsonPath("$.data.title").value("2026 春季测评"))
|
|
|
|
|
+ .andExpect(jsonPath("$.data.status").value("active"));
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Test
|
|
|
|
|
+ void getActiveMaterial_无启用材料_返回空data() throws Exception {
|
|
|
|
|
+ when(assessmentService.getActiveMaterial()).thenReturn(null);
|
|
|
|
|
+
|
|
|
|
|
+ mockMvc.perform(post(URL)
|
|
|
|
|
+ .contentType(MediaType.APPLICATION_JSON))
|
|
|
|
|
+ .andExpect(status().isOk())
|
|
|
|
|
+ .andExpect(jsonPath("$.code").value(200))
|
|
|
|
|
+ .andExpect(jsonPath("$.data").doesNotExist());
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Test
|
|
|
|
|
+ void getActiveMaterial_仅允许POST_GET请求返回405() throws Exception {
|
|
|
|
|
+ mockMvc.perform(get(URL))
|
|
|
|
|
+ .andExpect(status().isMethodNotAllowed());
|
|
|
|
|
+ }
|
|
|
|
|
+}
|