详细分析SpringBootTest中的测试类(附Demo)

时间:2025-03-20 11:13:11
package cn.example.module.dangerous.service.enterpriseregistry; import org.junit.jupiter.api.Disabled; import org.junit.jupiter.api.Test; import org.springframework.boot.test.mock.mockito.MockBean; import javax.annotation.Resource; import cn.example.framework.test.core.ut.BaseDbUnitTest; import cn.example.module.dangerous.controller.admin.enterpriseregistry.vo.*; import cn.example.module.dangerous.dal.dataobject.enterpriseregistry.EnterpriseRegistryDO; import cn.example.module.dangerous.dal.mysql.enterpriseregistry.EnterpriseRegistryMapper; import cn.example.framework.common.pojo.PageResult; import javax.annotation.Resource; import org.springframework.context.annotation.Import; import java.util.*; import java.time.LocalDateTime; import static cn.hutool.core.util.RandomUtil.*; import static cn.example.module.dangerous.enums.ErrorCodeConstants.*; import static cn.example.framework.test.core.util.AssertUtils.*; import static cn.example.framework.test.core.util.RandomUtils.*; import static cn.example.framework.common.util.date.LocalDateTimeUtils.*; import static cn.example.framework.common.util.object.ObjectUtils.*; import static cn.example.framework.common.util.date.DateUtils.*; import static org.junit.jupiter.api.Assertions.*; import static org.mockito.Mockito.*; /** * {@link EnterpriseRegistryServiceImpl} 的单元测试类 * * @author 管理员 */ @Import(EnterpriseRegistryServiceImpl.class) public class EnterpriseRegistryServiceImplTest extends BaseDbUnitTest { @Resource private EnterpriseRegistryServiceImpl enterpriseRegistryService; @Resource private EnterpriseRegistryMapper enterpriseRegistryMapper; @Test public void testCreateEnterpriseRegistry_success() { // 准备参数 EnterpriseRegistrySaveReqVO createReqVO = randomPojo(EnterpriseRegistrySaveReqVO.class).setId(null); // 调用 Long enterpriseRegistryId = enterpriseRegistryService.createEnterpriseRegistry(createReqVO); // 断言 assertNotNull(enterpriseRegistryId); // 校验记录的属性是否正确 EnterpriseRegistryDO enterpriseRegistry = enterpriseRegistryMapper.selectById(enterpriseRegistryId); assertPojoEquals(createReqVO, enterpriseRegistry, "id"); } @Test public void testUpdateEnterpriseRegistry_success() { // mock 数据 EnterpriseRegistryDO dbEnterpriseRegistry = randomPojo(EnterpriseRegistryDO.class); enterpriseRegistryMapper.insert(dbEnterpriseRegistry);// @Sql: 先插入出一条存在的数据 // 准备参数 EnterpriseRegistrySaveReqVO updateReqVO = randomPojo(EnterpriseRegistrySaveReqVO.class, o -> { o.setId(dbEnterpriseRegistry.getId()); // 设置更新的 ID }); // 调用 enterpriseRegistryService.updateEnterpriseRegistry(updateReqVO); // 校验是否更新正确 EnterpriseRegistryDO enterpriseRegistry = enterpriseRegistryMapper.selectById(updateReqVO.getId()); // 获取最新的 assertPojoEquals(updateReqVO, enterpriseRegistry); } @Test public void testUpdateEnterpriseRegistry_notExists() { // 准备参数 EnterpriseRegistrySaveReqVO updateReqVO = randomPojo(EnterpriseRegistrySaveReqVO.class); // 调用, 并断言异常 assertServiceException(() -> enterpriseRegistryService.updateEnterpriseRegistry(updateReqVO), ENTERPRISE_REGISTRY_NOT_EXISTS); } @Test public void testDeleteEnterpriseRegistry_success() { // mock 数据 EnterpriseRegistryDO dbEnterpriseRegistry = randomPojo(EnterpriseRegistryDO.class); enterpriseRegistryMapper.insert(dbEnterpriseRegistry);// @Sql: 先插入出一条存在的数据 // 准备参数 Long id = dbEnterpriseRegistry.getId(); // 调用 enterpriseRegistryService.deleteEnterpriseRegistry(id); // 校验数据不存在了 assertNull(enterpriseRegistryMapper.selectById(id)); } @Test public void testDeleteEnterpriseRegistry_notExists() { // 准备参数 Long id = randomLongId(); // 调用, 并断言异常 assertServiceException(() -> enterpriseRegistryService.deleteEnterpriseRegistry(id), ENTERPRISE_REGISTRY_NOT_EXISTS); } @Test @Disabled // TODO 请修改 null 为需要的值,然后删除 @Disabled 注解 public void testGetEnterpriseRegistryPage() { // mock 数据 EnterpriseRegistryDO dbEnterpriseRegistry = randomPojo(EnterpriseRegistryDO.class, o -> { // 等会查询到 o.setEnterpriseName(null); o.setCreditCode(null); o.setRegistrant(null); o.setCreateTime(null); o.setEnterpriseCode(null); o.setContactNumber(null); }); enterpriseRegistryMapper.insert(dbEnterpriseRegistry); // 测试 enterpriseName 不匹配 enterpriseRegistryMapper.insert(cloneIgnoreId(dbEnterpriseRegistry, o -> o.setEnterpriseName(null))); // 测试 creditCode 不匹配 enterpriseRegistryMapper.insert(cloneIgnoreId(dbEnterpriseRegistry, o -> o.setCreditCode(null))); // 测试 registrant 不匹配 enterpriseRegistryMapper.insert(cloneIgnoreId(dbEnterpriseRegistry, o -> o.setRegistrant(null))); // 测试 createTime 不匹配 enterpriseRegistryMapper.insert(cloneIgnoreId(dbEnterpriseRegistry, o -> o.setCreateTime(null))); // 测试 enterpriseCode 不匹配 enterpriseRegistryMapper.insert(cloneIgnoreId(dbEnterpriseRegistry, o -> o.setEnterpriseCode(null))); // 测试 contactNumber 不匹配 enterpriseRegistryMapper.insert(cloneIgnoreId(dbEnterpriseRegistry, o -> o.setContactNumber(null))); // 准备参数 EnterpriseRegistryPageReqVO reqVO = new EnterpriseRegistryPageReqVO(); reqVO.setEnterpriseName(null); reqVO.setCreditCode(null); reqVO.setRegistrant(null); reqVO.setCreateTime(buildBetweenTime(2023, 2, 1, 2023, 2, 28)); reqVO.setEnterpriseCode(null); reqVO.setContactNumber(null); // 调用 PageResult<EnterpriseRegistryDO> pageResult = enterpriseRegistryService.getEnterpriseRegistryPage(reqVO); // 断言 assertEquals(1, pageResult.getTotal()); assertEquals(1, pageResult.getList().size()); assertPojoEquals(dbEnterpriseRegistry, pageResult.getList().get(0)); } }