dengzedong
2024-12-19 85b2001c0ec2f1adc598db3bf47ad457dcca7074
提交 | 用户 | 时间
e7c126 1 package com.iailab.module.infra.service.demo;
H 2
3 import org.junit.jupiter.api.Disabled;
4 import org.junit.jupiter.api.Test;
5 import org.springframework.boot.test.mock.mockito.MockBean;
6
7 import javax.annotation.Resource;
8
9 import com.iailab.framework.test.core.ut.BaseDbUnitTest;
10
11 import com.iailab.module.infra.controller.admin.demo.vo.*;
12 import com.iailab.module.infra.dal.dataobject.demo.InfraCategoryDO;
13 import com.iailab.module.infra.dal.mysql.demo.InfraCategoryMapper;
14 import com.iailab.framework.common.pojo.PageResult;
15
16 import javax.annotation.Resource;
17 import org.springframework.context.annotation.Import;
18 import java.util.*;
19 import java.time.LocalDateTime;
20
21 import static cn.hutool.core.util.RandomUtil.*;
22 import static com.iailab.module.infra.enums.ErrorCodeConstants.*;
23 import static com.iailab.framework.test.core.util.AssertUtils.*;
24 import static com.iailab.framework.test.core.util.RandomUtils.*;
25 import static com.iailab.framework.common.util.date.LocalDateTimeUtils.*;
26 import static com.iailab.framework.common.util.object.ObjectUtils.*;
27 import static com.iailab.framework.common.util.date.DateUtils.*;
28 import static org.junit.jupiter.api.Assertions.*;
29 import static org.mockito.Mockito.*;
30
31 /**
32  * {@link InfraCategoryServiceImpl} 的单元测试类
33  *
34  * @author iailab
35  */
36 @Import(InfraCategoryServiceImpl.class)
37 public class InfraCategoryServiceImplTest extends BaseDbUnitTest {
38
39     @Resource
40     private InfraCategoryServiceImpl categoryService;
41
42     @Resource
43     private InfraCategoryMapper categoryMapper;
44
45     @Test
46     public void testCreateCategory_success() {
47         // 准备参数
48         InfraCategorySaveReqVO createReqVO = randomPojo(InfraCategorySaveReqVO.class).setId(null);
49
50         // 调用
51         Long categoryId = categoryService.createCategory(createReqVO);
52         // 断言
53         assertNotNull(categoryId);
54         // 校验记录的属性是否正确
55         InfraCategoryDO category = categoryMapper.selectById(categoryId);
56         assertPojoEquals(createReqVO, category, "id");
57     }
58
59     @Test
60     public void testUpdateCategory_success() {
61         // mock 数据
62         InfraCategoryDO dbCategory = randomPojo(InfraCategoryDO.class);
63         categoryMapper.insert(dbCategory);// @Sql: 先插入出一条存在的数据
64         // 准备参数
65         InfraCategorySaveReqVO updateReqVO = randomPojo(InfraCategorySaveReqVO.class, o -> {
66             o.setId(dbCategory.getId()); // 设置更新的 ID
67         });
68
69         // 调用
70         categoryService.updateCategory(updateReqVO);
71         // 校验是否更新正确
72         InfraCategoryDO category = categoryMapper.selectById(updateReqVO.getId()); // 获取最新的
73         assertPojoEquals(updateReqVO, category);
74     }
75
76     @Test
77     public void testUpdateCategory_notExists() {
78         // 准备参数
79         InfraCategorySaveReqVO updateReqVO = randomPojo(InfraCategorySaveReqVO.class);
80
81         // 调用, 并断言异常
82         assertServiceException(() -> categoryService.updateCategory(updateReqVO), CATEGORY_NOT_EXISTS);
83     }
84
85     @Test
86     public void testDeleteCategory_success() {
87         // mock 数据
88         InfraCategoryDO dbCategory = randomPojo(InfraCategoryDO.class);
89         categoryMapper.insert(dbCategory);// @Sql: 先插入出一条存在的数据
90         // 准备参数
91         Long id = dbCategory.getId();
92
93         // 调用
94         categoryService.deleteCategory(id);
95        // 校验数据不存在了
96        assertNull(categoryMapper.selectById(id));
97     }
98
99     @Test
100     public void testDeleteCategory_notExists() {
101         // 准备参数
102         Long id = randomLongId();
103
104         // 调用, 并断言异常
105         assertServiceException(() -> categoryService.deleteCategory(id), CATEGORY_NOT_EXISTS);
106     }
107
108     @Test
109     @Disabled  // TODO 请修改 null 为需要的值,然后删除 @Disabled 注解
110     public void testGetCategoryList() {
111        // mock 数据
112        InfraCategoryDO dbCategory = randomPojo(InfraCategoryDO.class, o -> { // 等会查询到
113            o.setName(null);
114        });
115        categoryMapper.insert(dbCategory);
116        // 测试 name 不匹配
117        categoryMapper.insert(cloneIgnoreId(dbCategory, o -> o.setName(null)));
118        // 准备参数
119        InfraCategoryListReqVO reqVO = new InfraCategoryListReqVO();
120        reqVO.setName(null);
121
122        // 调用
123        List<InfraCategoryDO> list = categoryService.getCategoryList(reqVO);
124        // 断言
125        assertEquals(1, list.size());
126        assertPojoEquals(dbCategory, list.get(0));
127     }
128
129 }