mirror of
https://gitee.com/hhyykk/ipms-sjy.git
synced 2025-06-20 23:42:00 +08:00
112 lines
4.3 KiB
Plaintext
112 lines
4.3 KiB
Plaintext
![]() |
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});
|
||
|
}
|
||
|
|
||
|
}
|