FamilyWorkbenchPlanWriteTest.java 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. package com.etotem.cfc.service;
  2. import com.etotem.cfc.dto.FamilyPlanCreateDTO;
  3. import com.etotem.cfc.entity.HealthPlan;
  4. import com.etotem.cfc.mapper.HealthPlanMapper;
  5. import com.etotem.cfc.service.impl.HealthPlanServiceImpl;
  6. import org.junit.jupiter.api.BeforeEach;
  7. import org.junit.jupiter.api.Test;
  8. import org.mockito.ArgumentCaptor;
  9. import org.mockito.InjectMocks;
  10. import org.mockito.Mock;
  11. import org.mockito.MockitoAnnotations;
  12. import static org.junit.jupiter.api.Assertions.assertEquals;
  13. import static org.junit.jupiter.api.Assertions.assertThrows;
  14. import static org.junit.jupiter.api.Assertions.assertTrue;
  15. import static org.mockito.ArgumentMatchers.any;
  16. import static org.mockito.Mockito.verify;
  17. import static org.mockito.Mockito.when;
  18. /**
  19. * 方案新增/编辑契约:create 固定 family_id/status 默认 active;update 校验归属(防跨家庭越权)。
  20. */
  21. class FamilyWorkbenchPlanWriteTest {
  22. @Mock private HealthPlanMapper planMapper;
  23. @InjectMocks private HealthPlanServiceImpl service;
  24. @BeforeEach
  25. void setUp() {
  26. MockitoAnnotations.openMocks(this);
  27. }
  28. private FamilyPlanCreateDTO dto() {
  29. FamilyPlanCreateDTO d = new FamilyPlanCreateDTO();
  30. d.setPlanType("gut");
  31. d.setMemberName("小明");
  32. d.setGoal("改善肠道菌群");
  33. d.setTotalDays(30);
  34. return d;
  35. }
  36. @Test
  37. void 创建方案_familyId固定_status默认active_返回id() {
  38. when(planMapper.insert(any(HealthPlan.class))).thenAnswer(inv -> {
  39. HealthPlan p = inv.getArgument(0);
  40. p.setId(321L);
  41. return 1;
  42. });
  43. Long id = service.createFamilyPlan(5L, 88L, dto());
  44. ArgumentCaptor<HealthPlan> cap = ArgumentCaptor.forClass(HealthPlan.class);
  45. verify(planMapper).insert(cap.capture());
  46. HealthPlan p = cap.getValue();
  47. assertEquals(5L, p.getFamilyId());
  48. assertEquals("active", p.getStatus());
  49. assertEquals("gut", p.getPlanType());
  50. assertEquals("改善肠道菌群", p.getGoal());
  51. assertEquals(Integer.valueOf(30), p.getTotalDays());
  52. assertEquals(321L, id);
  53. }
  54. @Test
  55. void 编辑方案_跨家庭_拒绝() {
  56. HealthPlan other = new HealthPlan();
  57. other.setId(55L);
  58. other.setFamilyId(9L);
  59. when(planMapper.selectById(55L)).thenReturn(other);
  60. RuntimeException e = assertThrows(RuntimeException.class,
  61. () -> service.updateFamilyPlan(55L, 5L, dto()));
  62. assertTrue(e.getMessage().contains("方案不属于该家庭"));
  63. }
  64. @Test
  65. void 编辑方案_同家庭_字段更新() {
  66. HealthPlan p = new HealthPlan();
  67. p.setId(55L);
  68. p.setFamilyId(5L);
  69. p.setGoal("旧目标");
  70. when(planMapper.selectById(55L)).thenReturn(p);
  71. when(planMapper.updateById(any(HealthPlan.class))).thenReturn(1);
  72. boolean ok = service.updateFamilyPlan(55L, 5L, dto());
  73. verify(planMapper).updateById(p);
  74. assertTrue(ok);
  75. assertEquals("改善肠道菌群", p.getGoal());
  76. }
  77. }