parent
e8c4837c54
commit
7516f53be0
@ -1,146 +0,0 @@
|
||||
package com.chanko.yunxi.mes.module.heli.service.client;
|
||||
|
||||
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 com.chanko.yunxi.mes.framework.test.core.ut.BaseDbUnitTest;
|
||||
|
||||
import com.chanko.yunxi.mes.module.heli.controller.admin.client.vo.*;
|
||||
import com.chanko.yunxi.mes.module.heli.dal.dataobject.client.ClientDO;
|
||||
import com.chanko.yunxi.mes.module.heli.dal.mysql.client.ClientMapper;
|
||||
import com.chanko.yunxi.mes.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 com.chanko.yunxi.mes.module.heli.enums.ErrorCodeConstants.*;
|
||||
import static com.chanko.yunxi.mes.framework.test.core.util.AssertUtils.*;
|
||||
import static com.chanko.yunxi.mes.framework.test.core.util.RandomUtils.*;
|
||||
import static com.chanko.yunxi.mes.framework.common.util.date.LocalDateTimeUtils.*;
|
||||
import static com.chanko.yunxi.mes.framework.common.util.object.ObjectUtils.*;
|
||||
import static com.chanko.yunxi.mes.framework.common.util.date.DateUtils.*;
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
import static org.mockito.Mockito.*;
|
||||
|
||||
/**
|
||||
* {@link ClientServiceImpl} 的单元测试类
|
||||
*
|
||||
* @author 开发者
|
||||
*/
|
||||
@Import(ClientServiceImpl.class)
|
||||
public class ClientServiceImplTest extends BaseDbUnitTest {
|
||||
|
||||
@Resource
|
||||
private ClientServiceImpl clientService;
|
||||
|
||||
@Resource
|
||||
private ClientMapper clientMapper;
|
||||
|
||||
@Test
|
||||
public void testCreateClient_success() {
|
||||
// 准备参数
|
||||
ClientSaveReqVO createReqVO = randomPojo(ClientSaveReqVO.class).setId(null);
|
||||
|
||||
// 调用
|
||||
Long clientId = clientService.createClient(createReqVO);
|
||||
// 断言
|
||||
assertNotNull(clientId);
|
||||
// 校验记录的属性是否正确
|
||||
ClientDO client = clientMapper.selectById(clientId);
|
||||
assertPojoEquals(createReqVO, client, "id");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testUpdateClient_success() {
|
||||
// mock 数据
|
||||
ClientDO dbClient = randomPojo(ClientDO.class);
|
||||
clientMapper.insert(dbClient);// @Sql: 先插入出一条存在的数据
|
||||
// 准备参数
|
||||
ClientSaveReqVO updateReqVO = randomPojo(ClientSaveReqVO.class, o -> {
|
||||
o.setId(dbClient.getId()); // 设置更新的 ID
|
||||
});
|
||||
|
||||
// 调用
|
||||
clientService.updateClient(updateReqVO);
|
||||
// 校验是否更新正确
|
||||
ClientDO client = clientMapper.selectById(updateReqVO.getId()); // 获取最新的
|
||||
assertPojoEquals(updateReqVO, client);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testUpdateClient_notExists() {
|
||||
// 准备参数
|
||||
ClientSaveReqVO updateReqVO = randomPojo(ClientSaveReqVO.class);
|
||||
|
||||
// 调用, 并断言异常
|
||||
assertServiceException(() -> clientService.updateClient(updateReqVO), CLIENT_NOT_EXISTS);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDeleteClient_success() {
|
||||
// mock 数据
|
||||
ClientDO dbClient = randomPojo(ClientDO.class);
|
||||
clientMapper.insert(dbClient);// @Sql: 先插入出一条存在的数据
|
||||
// 准备参数
|
||||
Long id = dbClient.getId();
|
||||
|
||||
// 调用
|
||||
clientService.deleteClient(id);
|
||||
// 校验数据不存在了
|
||||
assertNull(clientMapper.selectById(id));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDeleteClient_notExists() {
|
||||
// 准备参数
|
||||
Long id = randomLongId();
|
||||
|
||||
// 调用, 并断言异常
|
||||
assertServiceException(() -> clientService.deleteClient(id), CLIENT_NOT_EXISTS);
|
||||
}
|
||||
|
||||
@Test
|
||||
@Disabled // TODO 请修改 null 为需要的值,然后删除 @Disabled 注解
|
||||
public void testGetClientPage() {
|
||||
// mock 数据
|
||||
ClientDO dbClient = randomPojo(ClientDO.class, o -> { // 等会查询到
|
||||
o.setCode(null);
|
||||
o.setSimpleName(null);
|
||||
o.setName(null);
|
||||
o.setTelephone(null);
|
||||
o.setStatus(null);
|
||||
});
|
||||
clientMapper.insert(dbClient);
|
||||
// 测试 code 不匹配
|
||||
clientMapper.insert(cloneIgnoreId(dbClient, o -> o.setCode(null)));
|
||||
// 测试 simpleName 不匹配
|
||||
clientMapper.insert(cloneIgnoreId(dbClient, o -> o.setSimpleName(null)));
|
||||
// 测试 name 不匹配
|
||||
clientMapper.insert(cloneIgnoreId(dbClient, o -> o.setName(null)));
|
||||
// 测试 telephone 不匹配
|
||||
clientMapper.insert(cloneIgnoreId(dbClient, o -> o.setTelephone(null)));
|
||||
// 测试 status 不匹配
|
||||
clientMapper.insert(cloneIgnoreId(dbClient, o -> o.setStatus(null)));
|
||||
// 准备参数
|
||||
ClientPageReqVO reqVO = new ClientPageReqVO();
|
||||
reqVO.setCode(null);
|
||||
reqVO.setSimpleName(null);
|
||||
reqVO.setName(null);
|
||||
reqVO.setTelephone(null);
|
||||
reqVO.setStatus(null);
|
||||
|
||||
// 调用
|
||||
PageResult<ClientDO> pageResult = clientService.getClientPage(reqVO);
|
||||
// 断言
|
||||
assertEquals(1, pageResult.getTotal());
|
||||
assertEquals(1, pageResult.getList().size());
|
||||
assertPojoEquals(dbClient, pageResult.getList().get(0));
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in new issue