parent
ea88210243
commit
aaf762e74f
@ -0,0 +1,111 @@
|
|||||||
|
package ${basePackage}.modules.${table.moduleName}.service.${table.businessName};
|
||||||
|
|
||||||
|
import ${basePackage}.BaseSpringBootUnitTest;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
import org.springframework.boot.test.mock.mockito.MockBean;
|
||||||
|
|
||||||
|
import javax.annotation.Resource;
|
||||||
|
|
||||||
|
import cn.iocoder.dashboard.BaseSpringBootUnitTest;
|
||||||
|
import ${basePackage}.modules.${table.moduleName}.service.${table.businessName}.impl.${table.className}ServiceImpl;
|
||||||
|
import ${basePackage}.modules.${table.moduleName}.controller.${table.businessName}.vo.*;
|
||||||
|
import ${basePackage}.modules.${table.moduleName}.dal.dataobject.${table.businessName}.${table.className}DO;
|
||||||
|
import ${basePackage}.modules.${table.moduleName}.dal.mysql.${table.businessName}.${table.className}Mapper;
|
||||||
|
|
||||||
|
import javax.annotation.Resource;
|
||||||
|
|
||||||
|
import static cn.hutool.core.util.RandomUtil.*;
|
||||||
|
import static ${basePackage}.modules.${table.moduleName}.enums.${simpleModuleName_upperFirst}ErrorCodeConstants.*;
|
||||||
|
import static cn.iocoder.dashboard.util.AssertUtils.*;
|
||||||
|
import static cn.iocoder.dashboard.util.RandomUtils.*;
|
||||||
|
import static cn.iocoder.dashboard.util.date.DateUtils.*;
|
||||||
|
import static org.junit.jupiter.api.Assertions.*;
|
||||||
|
import static org.mockito.Mockito.*;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@link ${table.className}ServiceImpl} 的单元测试类
|
||||||
|
*
|
||||||
|
* @author ${table.author}
|
||||||
|
*/
|
||||||
|
public class ${table.className}ServiceTest extends BaseSpringBootUnitTest {
|
||||||
|
|
||||||
|
@Resource
|
||||||
|
private ${table.className}ServiceImpl ${classNameVar}Service;
|
||||||
|
|
||||||
|
@Resource
|
||||||
|
private ${table.className}Mapper ${classNameVar}Mapper;
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testCreate${simpleClassName}_success() {
|
||||||
|
// 准备参数
|
||||||
|
${table.className}CreateReqVO reqVO = randomPojo(${table.className}CreateReqVO.class);
|
||||||
|
|
||||||
|
// 调用
|
||||||
|
Long ${classNameVar}Id = ${classNameVar}Service.create${simpleClassName}(reqVO);
|
||||||
|
// 断言
|
||||||
|
assertNotNull(${classNameVar}Id);
|
||||||
|
// 校验记录的属性是否正确
|
||||||
|
${table.className}DO ${classNameVar} = ${classNameVar}Mapper.selectById(${classNameVar}Id);
|
||||||
|
assertPojoEquals(reqVO, ${classNameVar});
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testUpdate${simpleClassName}_success() {
|
||||||
|
// mock 数据
|
||||||
|
${table.className}DO db${simpleClassName} = randomPojo(${table.className}DO.class);
|
||||||
|
${classNameVar}Mapper.insert(db${simpleClassName});// @Sql: 先插入出一条存在的数据
|
||||||
|
// 准备参数
|
||||||
|
${table.className}UpdateReqVO reqVO = randomPojo(${table.className}UpdateReqVO.class, o -> {
|
||||||
|
o.setId(db${simpleClassName}.getId()); // 设置更新的 ID
|
||||||
|
});
|
||||||
|
|
||||||
|
// 调用
|
||||||
|
${classNameVar}Service.update${simpleClassName}(reqVO);
|
||||||
|
// 校验是否更新正确
|
||||||
|
${table.className}DO ${classNameVar} = ${classNameVar}Mapper.selectById(reqVO.getId()); // 获取最新的
|
||||||
|
assertPojoEquals(reqVO, ${classNameVar});
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testUpdate${simpleClassName}_notExists() {
|
||||||
|
// 准备参数
|
||||||
|
${table.className}UpdateReqVO reqVO = randomPojo(${table.className}UpdateReqVO.class);
|
||||||
|
|
||||||
|
// 调用, 并断言异常
|
||||||
|
assertServiceException(() -> ${classNameVar}Service.update${simpleClassName}(reqVO), ${simpleClassName_underlineCase.toUpperCase()}_NOT_EXISTS);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testDelete${simpleClassName}_success() {
|
||||||
|
// mock 数据
|
||||||
|
${table.className}DO db${simpleClassName} = randomPojo(${table.className}DO.class);
|
||||||
|
${classNameVar}Mapper.insert(db${simpleClassName});// @Sql: 先插入出一条存在的数据
|
||||||
|
// 准备参数
|
||||||
|
Long id = db${simpleClassName}.getId();
|
||||||
|
|
||||||
|
// 调用
|
||||||
|
${classNameVar}Service.delete${simpleClassName}(id);
|
||||||
|
// 校验数据不存在了
|
||||||
|
assertNull(configMapper.selectById(id));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testDelete${simpleClassName}_notExists() {
|
||||||
|
// 准备参数
|
||||||
|
Long id = randomLongId();
|
||||||
|
|
||||||
|
// 调用, 并断言异常
|
||||||
|
assertServiceException(() -> ${classNameVar}Service.delete${simpleClassName}(id), ${simpleClassName_underlineCase.toUpperCase()}_NOT_EXISTS);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testGet${simpleClassName}Page() {
|
||||||
|
// mock 数据
|
||||||
|
${table.className}DO db${simpleClassName} = randomPojo(${table.className}DO.class, o -> {); // 等会查询到
|
||||||
|
|
||||||
|
});
|
||||||
|
${classNameVar}Mapper.insert(db${simpleClassName});
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@ -0,0 +1,97 @@
|
|||||||
|
package cn.iocoder.dashboard.modules.infra.service.job;
|
||||||
|
|
||||||
|
import cn.iocoder.dashboard.BaseSpringBootUnitTest;
|
||||||
|
import cn.iocoder.dashboard.modules.infra.controller.config.vo.InfConfigCreateReqVO;
|
||||||
|
import cn.iocoder.dashboard.modules.infra.controller.config.vo.InfConfigUpdateReqVO;
|
||||||
|
import cn.iocoder.dashboard.modules.infra.dal.dataobject.config.InfConfigDO;
|
||||||
|
import cn.iocoder.dashboard.modules.infra.dal.mysql.config.InfConfigMapper;
|
||||||
|
import cn.iocoder.dashboard.modules.infra.service.config.impl.InfConfigServiceImpl;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
import javax.annotation.Resource;
|
||||||
|
|
||||||
|
import static cn.iocoder.dashboard.modules.infra.enums.InfErrorCodeConstants.CONFIG_NOT_EXISTS;
|
||||||
|
import static cn.iocoder.dashboard.util.AssertUtils.assertPojoEquals;
|
||||||
|
import static cn.iocoder.dashboard.util.AssertUtils.assertServiceException;
|
||||||
|
import static cn.iocoder.dashboard.util.RandomUtils.randomLongId;
|
||||||
|
import static cn.iocoder.dashboard.util.RandomUtils.randomPojo;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertNull;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@link InfConfigServiceImpl} 的单元测试类
|
||||||
|
*
|
||||||
|
* @author 芋艿
|
||||||
|
*/
|
||||||
|
public class InfConfigServiceTest extends BaseSpringBootUnitTest {
|
||||||
|
|
||||||
|
@Resource
|
||||||
|
private InfConfigServiceImpl configService;
|
||||||
|
|
||||||
|
@Resource
|
||||||
|
private InfConfigMapper configMapper;
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testCreateConfig_success() {
|
||||||
|
// 准备参数
|
||||||
|
InfConfigCreateReqVO reqVO = randomPojo(InfConfigCreateReqVO.class);
|
||||||
|
|
||||||
|
// 调用
|
||||||
|
Long configId = configService.createConfig(reqVO);
|
||||||
|
// 断言
|
||||||
|
assertNotNull(configId);
|
||||||
|
// 校验记录的属性是否正确
|
||||||
|
InfConfigDO config = configMapper.selectById(configId);
|
||||||
|
assertPojoEquals(reqVO, config);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testUpdateConfig_success() {
|
||||||
|
// mock 数据
|
||||||
|
InfConfigDO dbConfig = randomPojo(InfConfigDO.class);
|
||||||
|
configMapper.insert(dbConfig);// @Sql: 先插入出一条存在的数据
|
||||||
|
// 准备参数
|
||||||
|
InfConfigUpdateReqVO reqVO = randomPojo(InfConfigUpdateReqVO.class, o -> {
|
||||||
|
o.setId(dbConfig.getId()); // 设置更新的 ID
|
||||||
|
});
|
||||||
|
|
||||||
|
// 调用
|
||||||
|
configService.updateConfig(reqVO);
|
||||||
|
// 校验是否更新正确
|
||||||
|
InfConfigDO config = configMapper.selectById(reqVO.getId()); // 获取最新的
|
||||||
|
assertPojoEquals(reqVO, config);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testUpdateConfig_notExists() {
|
||||||
|
// 准备参数
|
||||||
|
InfConfigUpdateReqVO reqVO = randomPojo(InfConfigUpdateReqVO.class);
|
||||||
|
|
||||||
|
// 调用, 并断言异常
|
||||||
|
assertServiceException(() -> configService.updateConfig(reqVO), CONFIG_NOT_EXISTS);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testDeleteConfig_success() {
|
||||||
|
// mock 数据
|
||||||
|
InfConfigDO dbConfig = randomPojo(InfConfigDO.class);
|
||||||
|
configMapper.insert(dbConfig);// @Sql: 先插入出一条存在的数据
|
||||||
|
// 准备参数
|
||||||
|
Long id = dbConfig.getId();
|
||||||
|
|
||||||
|
// 调用
|
||||||
|
configService.deleteConfig(id);
|
||||||
|
// 校验数据不存在了
|
||||||
|
assertNull(configMapper.selectById(id));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testDeleteConfig_notExists() {
|
||||||
|
// 准备参数
|
||||||
|
Long id = randomLongId();
|
||||||
|
|
||||||
|
// 调用, 并断言异常
|
||||||
|
assertServiceException(() -> configService.deleteConfig(id), CONFIG_NOT_EXISTS);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue