|
|
@@ -0,0 +1,57 @@
|
|
|
+package com.etotem.cfc.service;
|
|
|
+
|
|
|
+import com.etotem.cfc.entity.Family;
|
|
|
+import com.etotem.cfc.entity.User;
|
|
|
+import com.etotem.cfc.mapper.FamilyMapper;
|
|
|
+import com.etotem.cfc.mapper.UserMapper;
|
|
|
+import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
|
+import org.junit.jupiter.api.Test;
|
|
|
+import org.springframework.boot.test.context.SpringBootTest;
|
|
|
+import org.springframework.boot.test.mock.mockito.MockBean;
|
|
|
+import javax.annotation.Resource;
|
|
|
+import static org.mockito.ArgumentMatchers.any;
|
|
|
+import static org.mockito.Mockito.*;
|
|
|
+
|
|
|
+/**
|
|
|
+ * 验证 updateUserInfo 在 realName 变化时联动更新家庭名。
|
|
|
+ *
|
|
|
+ * 注意:@SpringBootTest 需显式指定 datasource URL,因为 application.yml 的默认值
|
|
|
+ * 指向 192.168.16.251(不可达),而本地开发机 MariaDB(127.0.0.1:3306/zxyj)
|
|
|
+ * 有完整的 zxyj schema 可引导上下文加载。
|
|
|
+ */
|
|
|
+@SpringBootTest(properties = {
|
|
|
+ "spring.datasource.url=jdbc:mysql://127.0.0.1:3306/zxyj?useUnicode=true&characterEncoding=utf8&serverTimezone=Asia/Shanghai&useSSL=false&allowPublicKeyRetrieval=true",
|
|
|
+ "spring.datasource.username=zxyj",
|
|
|
+ "spring.datasource.password=zxyj@123"
|
|
|
+})
|
|
|
+public class UserServiceFamilyNameLinkTest {
|
|
|
+ @Resource private UserService userService;
|
|
|
+ @MockBean private UserMapper userMapper;
|
|
|
+ @MockBean private FamilyMapper familyMapper;
|
|
|
+
|
|
|
+ @Test void realName变化_联动家庭名() {
|
|
|
+ User u = new User(); u.setId(10L); u.setRole("parent"); u.setRealName("旧名"); u.setFamilyId(7L);
|
|
|
+ when(userMapper.selectById(10L)).thenReturn(u);
|
|
|
+ Family f = new Family(); f.setId(7L); f.setName("旧名家庭"); f.setCreatorId(10L);
|
|
|
+ when(familyMapper.selectOne(any(LambdaQueryWrapper.class))).thenReturn(f);
|
|
|
+ when(userMapper.updateById(any())).thenReturn(1);
|
|
|
+
|
|
|
+ com.etotem.cfc.dto.UpdateUserDTO dto = new com.etotem.cfc.dto.UpdateUserDTO();
|
|
|
+ dto.setRealName("王五");
|
|
|
+ userService.updateUserInfo(10L, dto);
|
|
|
+
|
|
|
+ verify(familyMapper).updateById(argThat(fam -> "王五家庭".equals(fam.getName())));
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test void realName未变_不改家庭名() {
|
|
|
+ User u = new User(); u.setId(10L); u.setRole("parent"); u.setRealName("王五");
|
|
|
+ when(userMapper.selectById(10L)).thenReturn(u);
|
|
|
+ when(userMapper.updateById(any())).thenReturn(1);
|
|
|
+
|
|
|
+ com.etotem.cfc.dto.UpdateUserDTO dto = new com.etotem.cfc.dto.UpdateUserDTO();
|
|
|
+ dto.setRealName("王五");
|
|
|
+ userService.updateUserInfo(10L, dto);
|
|
|
+
|
|
|
+ verify(familyMapper, never()).updateById(any());
|
|
|
+ }
|
|
|
+}
|