编写用户管理单元测试

This commit is contained in:
刘名喜 2024-11-06 17:35:15 +08:00
parent a73a6e96a6
commit 39ebdece85

View File

@ -0,0 +1,106 @@
import com.alibaba.fastjson.JSON;
import com.ktg.RuoYiApplication;
import com.ktg.common.core.domain.entity.SysRole;
import com.ktg.common.core.domain.entity.SysUser;
import com.ktg.system.service.ISysRoleService;
import com.ktg.system.service.ISysUserService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;
/**
* 用户管理
*/
@RunWith(SpringRunner.class)
@SpringBootTest(classes = RuoYiApplication.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class SysUserUnitTest {
@Autowired
private ISysUserService userService;
@Autowired
private ISysRoleService roleService;
@Test
public void add() {
SysUser sysUser = new SysUser();
sysUser.setDeptId(118L);
sysUser.setUserName("unittest02");
sysUser.setNickName("单元测试用户02");
sysUser.setPassword("123456");
sysUser.setStatus("0");
sysUser.setRoleIds(new Long[]{105L});
userService.insertUser(sysUser);
System.out.println("add" + sysUser.getUserId()); // 117
}
@Test
public void list() {
List<SysUser> list = userService.selectUserList(new SysUser());
System.out.println("list" + JSON.toJSONString(list));
}
@Test
public void getInfo() {
System.out.println("getInfo" + JSON.toJSONString(userService.selectUserById(117L)));
}
@Test
public void edit() {
SysUser sysUser = new SysUser();
sysUser.setUserId(117L);
sysUser.setUserName("unittest02");
sysUser.setNickName("单元测试用户03");
sysUser.setPassword("123456");
sysUser.setStatus("0");
sysUser.setRoleIds(new Long[]{105L});
int count = userService.updateUser(sysUser);
System.out.println("edit" + count);
}
@Test
public void remove() {
Long[] ids = {117L};
int count = userService.deleteUserByIds(ids);
System.out.println("remove" + count);
}
@Test
public void resetPwd() {
SysUser sysUser = new SysUser();
sysUser.setUserId(117L);
sysUser.setPassword("123456");
int count = userService.resetPwd(sysUser);
System.out.println("resetPwd" + count);
}
@Test
public void changeStatus() {
SysUser sysUser = new SysUser();
sysUser.setUserId(117L);
sysUser.setStatus("1");
int count = userService.resetPwd(sysUser);
System.out.println("changeStatus" + count);
}
@Test
public void authRole() {
HashMap<String, Object> map = new HashMap<>();
SysUser user = userService.selectUserById(117L);
List<SysRole> roles = roleService.selectRolesByUserId(117L);
map.put("user", user);
map.put("roles", SysUser.isAdmin(117L) ? roles : roles.stream().filter(r -> !r.isAdmin()).collect(Collectors.toList()));
System.out.println("authRole" + JSON.toJSONString(map));
}
@Test
public void insertAuthRole() {
userService.insertUserAuth(117L, new Long[]{105L});
System.out.println("insertAuthRoleOK");
}
}