增加测试用例

This commit is contained in:
zlt
2019-01-31 13:46:54 +08:00
parent 96ac04d41e
commit bfb5cba0c8
2 changed files with 86 additions and 3 deletions
@@ -14,11 +14,9 @@ import com.central.common.utils.ExcelUtil;
import com.central.user.model.SysUserExcel; import com.central.user.model.SysUserExcel;
import org.apache.commons.collections4.MapUtils; import org.apache.commons.collections4.MapUtils;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cache.annotation.CacheEvict; import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut; import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable; import org.springframework.cache.annotation.Cacheable;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
import com.central.common.utils.SysUserUtil; import com.central.common.utils.SysUserUtil;
@@ -39,7 +37,6 @@ import javax.servlet.http.HttpServletResponse;
*/ */
@Slf4j @Slf4j
@RestController @RestController
@RefreshScope
@Api(tags = "用户模块api") @Api(tags = "用户模块api")
public class SysUserController { public class SysUserController {
@Autowired @Autowired
@@ -0,0 +1,86 @@
package central.user.controller;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.delete;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import com.central.UserCenterApp;
import static org.assertj.core.api.Assertions.*;
import org.junit.Before;
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.http.MediaType;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.context.WebApplicationContext;
/**
* SysUserControllerTest单元测试用例
*
* @author zlt
*/
@RunWith(SpringRunner.class)
@SpringBootTest(classes = {UserCenterApp.class})
public class SysUserControllerTest {
@Autowired
private WebApplicationContext wac;
private MockMvc mockMvc;
@Before
public void setup() {
mockMvc = MockMvcBuilders.webAppContextSetup(wac).build();
}
@Test
public void whenQuerySuccess() throws Exception {
String result = mockMvc.perform(
get("/users/name/admin")
.contentType(MediaType.APPLICATION_JSON_UTF8))
.andExpect(status().isOk())
.andReturn().getResponse().getContentAsString();
System.out.println(result);
}
@Test
public void whenGetInfoSuccess() throws Exception {
String result = mockMvc.perform(get("/users/1")
.contentType(MediaType.APPLICATION_JSON_UTF8))
.andExpect(status().isOk())
.andExpect(jsonPath("$.username").value("admin"))
.andReturn().getResponse().getContentAsString();
System.out.println(result);
}
@Test
public void whenGetInfoNotExists() throws Exception {
String result = mockMvc.perform(get("/users/9999")
.contentType(MediaType.APPLICATION_JSON_UTF8))
.andExpect(status().isOk())
.andReturn().getResponse().getContentAsString();
assertThat(result).isEmpty();
}
@Test
public void whenGetUrl404() throws Exception {
mockMvc.perform(get("/user/a")
.contentType(MediaType.APPLICATION_JSON_UTF8))
.andExpect(status().is4xxClientError());
}
@Test
@Transactional
public void whenDeleteSuccess() throws Exception {
mockMvc.perform(delete("/users/1")
.contentType(MediaType.APPLICATION_JSON_UTF8))
.andExpect(status().isOk());
}
}