|
| 1 | +package org.jeecg.modules.system.test; |
| 2 | + |
| 3 | +import com.alibaba.fastjson.JSON; |
| 4 | +import com.alibaba.fastjson.JSONObject; |
| 5 | +import com.baomidou.mybatisplus.extension.plugins.pagination.Page; |
| 6 | +import org.jeecg.common.api.vo.Result; |
| 7 | +import org.jeecg.common.modules.redis.client.JeecgRedisClient; |
| 8 | +import org.jeecg.common.util.RedisUtil; |
| 9 | +import org.jeecg.config.JeecgBaseConfig; |
| 10 | +import org.jeecg.modules.base.service.BaseCommonService; |
| 11 | +import org.jeecg.modules.system.controller.SysUserController; |
| 12 | +import org.jeecg.modules.system.entity.SysUser; |
| 13 | +import org.jeecg.modules.system.service.*; |
| 14 | +import org.junit.jupiter.api.Assertions; |
| 15 | +import org.junit.jupiter.api.Test; |
| 16 | +import org.springframework.beans.factory.annotation.Autowired; |
| 17 | +import org.springframework.beans.factory.annotation.Value; |
| 18 | +import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; |
| 19 | +import org.springframework.boot.test.mock.mockito.MockBean; |
| 20 | +import org.springframework.http.MediaType; |
| 21 | +import org.springframework.test.web.servlet.MockMvc; |
| 22 | + |
| 23 | +import java.util.ArrayList; |
| 24 | +import java.util.List; |
| 25 | + |
| 26 | +import static org.mockito.ArgumentMatchers.any; |
| 27 | +import static org.mockito.BDDMockito.given; |
| 28 | +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*; |
| 29 | + |
| 30 | +/** |
| 31 | + * 系统用户单元测试 |
| 32 | + */ |
| 33 | +@WebMvcTest(SysUserController.class) |
| 34 | +public class SysUserApiTest { |
| 35 | + |
| 36 | + @Autowired |
| 37 | + private MockMvc mockMvc; |
| 38 | + |
| 39 | + @MockBean |
| 40 | + private ISysUserService sysUserService; |
| 41 | + |
| 42 | + @MockBean |
| 43 | + private ISysDepartService sysDepartService; |
| 44 | + |
| 45 | + @MockBean |
| 46 | + private ISysUserRoleService sysUserRoleService; |
| 47 | + |
| 48 | + @MockBean |
| 49 | + private ISysUserDepartService sysUserDepartService; |
| 50 | + |
| 51 | + @MockBean |
| 52 | + private ISysDepartRoleUserService departRoleUserService; |
| 53 | + |
| 54 | + @MockBean |
| 55 | + private ISysDepartRoleService departRoleService; |
| 56 | + |
| 57 | + @MockBean |
| 58 | + private RedisUtil redisUtil; |
| 59 | + |
| 60 | + @Value("${jeecg.path.upload}") |
| 61 | + private String upLoadPath; |
| 62 | + |
| 63 | + @MockBean |
| 64 | + private BaseCommonService baseCommonService; |
| 65 | + |
| 66 | + @MockBean |
| 67 | + private ISysUserAgentService sysUserAgentService; |
| 68 | + |
| 69 | + @MockBean |
| 70 | + private ISysPositionService sysPositionService; |
| 71 | + |
| 72 | + @MockBean |
| 73 | + private ISysUserTenantService userTenantService; |
| 74 | + |
| 75 | + @MockBean |
| 76 | + private JeecgRedisClient jeecgRedisClient; |
| 77 | + |
| 78 | + @MockBean |
| 79 | + private JeecgBaseConfig jeecgBaseConfig; |
| 80 | + /** |
| 81 | + * 测试地址:实际使用时替换成你自己的地址 |
| 82 | + */ |
| 83 | + private final String BASE_URL = "/sys/user/"; |
| 84 | + |
| 85 | + /** |
| 86 | + * 测试用例:查询记录 |
| 87 | + */ |
| 88 | + @Test |
| 89 | + public void testQuery() throws Exception{ |
| 90 | + // 请求地址 |
| 91 | + String url = BASE_URL + "list"; |
| 92 | + |
| 93 | + Page<SysUser> sysUserPage = new Page<>(); |
| 94 | + SysUser sysUser = new SysUser(); |
| 95 | + sysUser.setUsername("admin"); |
| 96 | + List<SysUser> users = new ArrayList<>(); |
| 97 | + users.add(sysUser); |
| 98 | + sysUserPage.setRecords(users); |
| 99 | + sysUserPage.setCurrent(1); |
| 100 | + sysUserPage.setSize(10); |
| 101 | + sysUserPage.setTotal(1); |
| 102 | + |
| 103 | + given(this.sysUserService.queryPageList(any(), any(), any(), any())).willReturn(Result.OK(sysUserPage)); |
| 104 | + |
| 105 | + String result = mockMvc.perform(get(url)).andReturn().getResponse().getContentAsString(); |
| 106 | + JSONObject jsonObject = JSON.parseObject(result); |
| 107 | + Assertions.assertEquals("admin", jsonObject.getJSONObject("result").getJSONArray("records").getJSONObject(0).getString("username")); |
| 108 | + } |
| 109 | + |
| 110 | + /** |
| 111 | + * 测试用例:新增 |
| 112 | + */ |
| 113 | + @Test |
| 114 | + public void testAdd() throws Exception { |
| 115 | + // 请求地址 |
| 116 | + String url = BASE_URL + "add" ; |
| 117 | + |
| 118 | + JSONObject params = new JSONObject(); |
| 119 | + params.put("username", "wangwuTest"); |
| 120 | + params.put("password", "123456"); |
| 121 | + params.put("confirmpassword","123456"); |
| 122 | + params.put("realname", "单元测试"); |
| 123 | + params.put("activitiSync", "1"); |
| 124 | + params.put("userIdentity","1"); |
| 125 | + params.put("workNo","0025"); |
| 126 | + |
| 127 | + String result = mockMvc.perform(post(url).contentType(MediaType.APPLICATION_JSON_VALUE).content(params.toJSONString())) |
| 128 | + .andReturn().getResponse().getContentAsString(); |
| 129 | + JSONObject jsonObject = JSON.parseObject(result); |
| 130 | + Assertions.assertTrue(jsonObject.getBoolean("success")); |
| 131 | + } |
| 132 | + |
| 133 | + |
| 134 | + /** |
| 135 | + * 测试用例:修改 |
| 136 | + */ |
| 137 | + @Test |
| 138 | + public void testEdit() throws Exception { |
| 139 | + // 数据Id |
| 140 | + String dataId = "1331795062924374018"; |
| 141 | + // 请求地址 |
| 142 | + String url = BASE_URL + "edit"; |
| 143 | + |
| 144 | + JSONObject params = new JSONObject(); |
| 145 | + params.put("username", "wangwuTest"); |
| 146 | + params.put("realname", "单元测试1111"); |
| 147 | + params.put("activitiSync", "1"); |
| 148 | + params.put("userIdentity","1"); |
| 149 | + params.put("workNo","0025"); |
| 150 | + params.put("id",dataId); |
| 151 | + |
| 152 | + SysUser sysUser = new SysUser(); |
| 153 | + sysUser.setUsername("admin"); |
| 154 | + |
| 155 | + given(this.sysUserService.getById(any())).willReturn(sysUser); |
| 156 | + |
| 157 | + String result = mockMvc.perform(put(url).contentType(MediaType.APPLICATION_JSON_VALUE).content(params.toJSONString())) |
| 158 | + .andReturn().getResponse().getContentAsString(); |
| 159 | + JSONObject jsonObject = JSON.parseObject(result); |
| 160 | + Assertions.assertTrue(jsonObject.getBoolean("success")); |
| 161 | + } |
| 162 | + |
| 163 | + |
| 164 | + /** |
| 165 | + * 测试用例:删除 |
| 166 | + */ |
| 167 | + @Test |
| 168 | + public void testDelete() throws Exception { |
| 169 | + // 数据Id |
| 170 | + String dataId = "1331795062924374018"; |
| 171 | + // 请求地址 |
| 172 | + String url = BASE_URL + "delete" + "?id=" + dataId; |
| 173 | + String result = mockMvc.perform(delete(url)).andReturn().getResponse().getContentAsString(); |
| 174 | + JSONObject jsonObject = JSON.parseObject(result); |
| 175 | + Assertions.assertTrue(jsonObject.getBoolean("success")); |
| 176 | + } |
| 177 | +} |
0 commit comments