|
|
@@ -0,0 +1,63 @@
|
|
|
+package com.etotem.cfc.unit;
|
|
|
+
|
|
|
+import com.etotem.cfc.entity.Family;
|
|
|
+import com.etotem.cfc.entity.FamilyMember;
|
|
|
+import com.etotem.cfc.entity.User;
|
|
|
+import com.etotem.cfc.mapper.FamilyMapper;
|
|
|
+import com.etotem.cfc.mapper.FamilyMemberMapper;
|
|
|
+import com.etotem.cfc.mapper.UserMapper;
|
|
|
+import com.etotem.cfc.service.FamilyMemberService;
|
|
|
+import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
|
+import org.junit.jupiter.api.Test;
|
|
|
+import org.junit.jupiter.api.extension.ExtendWith;
|
|
|
+import org.mockito.InjectMocks;
|
|
|
+import org.mockito.Mock;
|
|
|
+import org.mockito.junit.jupiter.MockitoExtension;
|
|
|
+
|
|
|
+import java.util.Arrays;
|
|
|
+
|
|
|
+import static org.junit.jupiter.api.Assertions.*;
|
|
|
+import static org.mockito.ArgumentMatchers.any;
|
|
|
+import static org.mockito.ArgumentMatchers.eq;
|
|
|
+import static org.mockito.Mockito.*;
|
|
|
+
|
|
|
+@ExtendWith(MockitoExtension.class)
|
|
|
+public class FamilyMemberServiceRenameTest {
|
|
|
+
|
|
|
+ @Mock private FamilyMemberMapper familyMemberMapper;
|
|
|
+ @Mock private UserMapper userMapper;
|
|
|
+ @Mock private FamilyMapper familyMapper;
|
|
|
+
|
|
|
+ @InjectMocks private FamilyMemberService service;
|
|
|
+
|
|
|
+ private User user(Long id, Long familyId) {
|
|
|
+ User u = new User(); u.setId(id); u.setFamilyId(familyId); return u;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test void rename_管理员_成功更新() {
|
|
|
+ when(userMapper.selectById(10L)).thenReturn(user(10L, 7L));
|
|
|
+ Family f = new Family(); f.setId(7L); f.setName("旧家庭"); f.setCreatorId(10L);
|
|
|
+ when(familyMapper.selectById(7L)).thenReturn(f);
|
|
|
+
|
|
|
+ String out = service.renameFamily(10L, " 新家庭 ");
|
|
|
+ assertEquals("新家庭", out);
|
|
|
+ verify(familyMapper).updateById(argThat(fam -> "新家庭".equals(fam.getName())));
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test void rename_非管理员_抛错() {
|
|
|
+ when(userMapper.selectById(20L)).thenReturn(user(20L, 7L));
|
|
|
+ Family f = new Family(); f.setId(7L); f.setName("旧家庭"); f.setCreatorId(10L);
|
|
|
+ when(familyMapper.selectById(7L)).thenReturn(f);
|
|
|
+ when(familyMemberMapper.selectCount(any(LambdaQueryWrapper.class))).thenReturn(0L);
|
|
|
+
|
|
|
+ assertThrows(RuntimeException.class, () -> service.renameFamily(20L, "新家庭"));
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test void rename_空白名_抛错() {
|
|
|
+ assertThrows(RuntimeException.class, () -> service.renameFamily(10L, " "));
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test void rename_超长名_抛错() {
|
|
|
+ assertThrows(RuntimeException.class, () -> service.renameFamily(10L, "一二三四五六七八九十一二三四五六七八九十一"));
|
|
|
+ }
|
|
|
+}
|