抽离Mock请求的方法,过渡到jackson
This commit is contained in:
@@ -11,15 +11,15 @@ import java.io.Serializable;
|
|||||||
* @date : 2019/03/28 15:24
|
* @date : 2019/03/28 15:24
|
||||||
*/
|
*/
|
||||||
@Data
|
@Data
|
||||||
public class Response implements Serializable {
|
public class Response<T> implements Serializable {
|
||||||
private int code;
|
private int code;
|
||||||
private String msg;
|
private String msg;
|
||||||
private Object result;
|
private T result;
|
||||||
|
|
||||||
public Response() {
|
public Response() {
|
||||||
}
|
}
|
||||||
|
|
||||||
public Response(int code, String msg, Object result) {
|
public Response(int code, String msg, T result) {
|
||||||
this.code = code;
|
this.code = code;
|
||||||
this.msg = msg;
|
this.msg = msg;
|
||||||
this.result = result;
|
this.result = result;
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
package cn.celess.blog;
|
package cn.celess.blog;
|
||||||
|
|
||||||
|
|
||||||
import cn.celess.blog.entity.request.LoginReq;
|
import cn.celess.blog.entity.request.LoginReq;
|
||||||
import net.sf.json.JSONObject;
|
import net.sf.json.JSONObject;
|
||||||
import org.junit.After;
|
import org.junit.After;
|
||||||
@@ -8,23 +9,25 @@ import org.junit.Test;
|
|||||||
import org.junit.runner.RunWith;
|
import org.junit.runner.RunWith;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.boot.test.context.SpringBootTest;
|
import org.springframework.boot.test.context.SpringBootTest;
|
||||||
|
import org.springframework.http.MediaType;
|
||||||
import org.springframework.mock.web.MockHttpSession;
|
import org.springframework.mock.web.MockHttpSession;
|
||||||
import org.springframework.test.context.ActiveProfiles;
|
import org.springframework.test.context.ActiveProfiles;
|
||||||
import org.springframework.test.context.junit4.SpringRunner;
|
import org.springframework.test.context.junit4.SpringRunner;
|
||||||
import org.springframework.test.context.web.WebAppConfiguration;
|
import org.springframework.test.context.web.WebAppConfiguration;
|
||||||
import org.springframework.test.web.servlet.MockMvc;
|
import org.springframework.test.web.servlet.MockMvc;
|
||||||
import org.springframework.test.web.servlet.MvcResult;
|
import org.springframework.test.web.servlet.ResultActions;
|
||||||
import org.springframework.test.web.servlet.ResultHandler;
|
import org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder;
|
||||||
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
|
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
|
||||||
import org.springframework.test.web.servlet.result.MockMvcResultHandlers;
|
|
||||||
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
|
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
|
||||||
import org.springframework.web.context.WebApplicationContext;
|
import org.springframework.web.context.WebApplicationContext;
|
||||||
|
|
||||||
import javax.servlet.http.Cookie;
|
|
||||||
|
|
||||||
import java.util.UUID;
|
import java.util.UUID;
|
||||||
|
|
||||||
|
import static cn.celess.blog.enmu.ResponseEnum.SUCCESS;
|
||||||
import static org.junit.Assert.*;
|
import static org.junit.Assert.*;
|
||||||
|
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
|
||||||
|
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @Author: 小海
|
* @Author: 小海
|
||||||
@@ -40,6 +43,8 @@ public class BaseTest {
|
|||||||
protected MockMvc mockMvc;
|
protected MockMvc mockMvc;
|
||||||
protected final static String Code = "code";
|
protected final static String Code = "code";
|
||||||
protected final static String Result = "result";
|
protected final static String Result = "result";
|
||||||
|
private static String userToken = null;
|
||||||
|
private static String adminToken = null;
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
private WebApplicationContext wac;
|
private WebApplicationContext wac;
|
||||||
@@ -59,6 +64,7 @@ public class BaseTest {
|
|||||||
|
|
||||||
|
|
||||||
protected String adminLogin() {
|
protected String adminLogin() {
|
||||||
|
if (adminToken != null) return adminToken;
|
||||||
try {
|
try {
|
||||||
LoginReq req = new LoginReq();
|
LoginReq req = new LoginReq();
|
||||||
req.setEmail("a@celess.cn");
|
req.setEmail("a@celess.cn");
|
||||||
@@ -68,9 +74,9 @@ public class BaseTest {
|
|||||||
String str = mockMvc.perform(MockMvcRequestBuilders.post("/login").content(loginReq.toString()).contentType("application/json"))
|
String str = mockMvc.perform(MockMvcRequestBuilders.post("/login").content(loginReq.toString()).contentType("application/json"))
|
||||||
// .andDo(MockMvcResultHandlers.print())
|
// .andDo(MockMvcResultHandlers.print())
|
||||||
.andReturn().getResponse().getContentAsString();
|
.andReturn().getResponse().getContentAsString();
|
||||||
String token = JSONObject.fromObject(str).getJSONObject(Result).getString("token");
|
adminToken = JSONObject.fromObject(str).getJSONObject(Result).getString("token");
|
||||||
assertNotNull(token);
|
assertNotNull(adminToken);
|
||||||
return token;
|
return adminToken;
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
return null;
|
return null;
|
||||||
@@ -78,6 +84,7 @@ public class BaseTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
protected String userLogin() {
|
protected String userLogin() {
|
||||||
|
if (userToken != null) return userToken;
|
||||||
try {
|
try {
|
||||||
LoginReq req = new LoginReq();
|
LoginReq req = new LoginReq();
|
||||||
req.setEmail("zh56462271@qq.com");
|
req.setEmail("zh56462271@qq.com");
|
||||||
@@ -87,9 +94,9 @@ public class BaseTest {
|
|||||||
String str = mockMvc.perform(MockMvcRequestBuilders.post("/login").content(loginReq.toString()).contentType("application/json"))
|
String str = mockMvc.perform(MockMvcRequestBuilders.post("/login").content(loginReq.toString()).contentType("application/json"))
|
||||||
// .andDo(MockMvcResultHandlers.print())
|
// .andDo(MockMvcResultHandlers.print())
|
||||||
.andReturn().getResponse().getContentAsString();
|
.andReturn().getResponse().getContentAsString();
|
||||||
String token = JSONObject.fromObject(str).getJSONObject(Result).getString("token");
|
userToken = JSONObject.fromObject(str).getJSONObject(Result).getString("token");
|
||||||
assertNotNull(token);
|
assertNotNull(userToken);
|
||||||
return token;
|
return userToken;
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
return null;
|
return null;
|
||||||
@@ -104,4 +111,24 @@ public class BaseTest {
|
|||||||
protected String randomStr(int len) {
|
protected String randomStr(int len) {
|
||||||
return UUID.randomUUID().toString().replaceAll("-", "").substring(0, len);
|
return UUID.randomUUID().toString().replaceAll("-", "").substring(0, len);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
protected ResultActions getMockData(String url) throws Exception {
|
||||||
|
return getMockData(url, null, null);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected ResultActions getMockData(String url, String token) throws Exception {
|
||||||
|
return getMockData(url, token, null);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected ResultActions getMockData(String url, String token, Object content) throws Exception {
|
||||||
|
MockHttpServletRequestBuilder mockHttpServletRequestBuilder = get(url);
|
||||||
|
if (token != null) {
|
||||||
|
mockHttpServletRequestBuilder.header("Authorization", token);
|
||||||
|
}
|
||||||
|
if (content != null) {
|
||||||
|
mockHttpServletRequestBuilder.content(JSONObject.fromObject(content).toString()).contentType(MediaType.APPLICATION_JSON);
|
||||||
|
}
|
||||||
|
return mockMvc.perform(mockHttpServletRequestBuilder).andExpect(status().isOk());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -8,6 +8,8 @@ import cn.celess.blog.entity.model.ArticleModel;
|
|||||||
import cn.celess.blog.entity.model.PageData;
|
import cn.celess.blog.entity.model.PageData;
|
||||||
import cn.celess.blog.entity.request.ArticleReq;
|
import cn.celess.blog.entity.request.ArticleReq;
|
||||||
import cn.celess.blog.mapper.ArticleMapper;
|
import cn.celess.blog.mapper.ArticleMapper;
|
||||||
|
import com.fasterxml.jackson.core.type.TypeReference;
|
||||||
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
import net.sf.json.JSONObject;
|
import net.sf.json.JSONObject;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
@@ -260,55 +262,39 @@ public class ArticleControllerTest extends BaseTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void adminArticles() {
|
public void adminArticles() {
|
||||||
String token;
|
ObjectMapper mapper = new ObjectMapper();
|
||||||
try {
|
try {
|
||||||
// 未登录
|
getMockData("/admin/articles?page=1&count=10").andExpect(result ->
|
||||||
mockMvc.perform(get("/admin/articles?page=1&count=10"))
|
assertEquals(HAVE_NOT_LOG_IN.getCode(), mapper.readValue(result.getResponse().getContentAsString(), Response.class).getCode())
|
||||||
.andExpect(status().isOk())
|
);
|
||||||
.andDo(result -> {
|
|
||||||
assertEquals(HAVE_NOT_LOG_IN.getCode(),
|
|
||||||
JSONObject.fromObject(result.getResponse().getContentAsString()).getInt(Code)
|
|
||||||
);
|
|
||||||
});
|
|
||||||
|
|
||||||
// User权限登陆
|
// User权限登陆
|
||||||
token = userLogin();
|
getMockData("/admin/articles?page=1&count=10", userLogin()).andDo(result ->
|
||||||
mockMvc.perform(get("/admin/articles?page=1&count=10")
|
assertEquals(PERMISSION_ERROR.getCode(), mapper.readValue(result.getResponse().getContentAsString(), Response.class).getCode())
|
||||||
.header("Authorization", token))
|
);
|
||||||
.andExpect(status().isOk())
|
|
||||||
.andDo(result -> {
|
|
||||||
JSONObject object = JSONObject.fromObject(result.getResponse().getContentAsString());
|
|
||||||
assertEquals(PERMISSION_ERROR.getCode(), object.getInt(Code));
|
|
||||||
});
|
|
||||||
|
|
||||||
token = adminLogin();
|
|
||||||
// admin权限登陆
|
// admin权限登陆
|
||||||
mockMvc.perform(get("/admin/articles?page=1&count=10")
|
getMockData("/admin/articles?page=1&count=10", adminLogin()).andDo(result -> {
|
||||||
.header("Authorization", token))
|
Response<PageData<ArticleModel>> response = mapper.readValue(result.getResponse().getContentAsString(), new TypeReference<Response<PageData<ArticleModel>>>(){});
|
||||||
.andExpect(status().isOk())
|
assertEquals(SUCCESS.getCode(), response.getCode());
|
||||||
.andDo(result -> {
|
assertNotNull(response.getResult());
|
||||||
JSONObject adminLogin = JSONObject.fromObject(result.getResponse().getContentAsString());
|
// 判断pageInfo是否包装完全
|
||||||
assertEquals(SUCCESS.getCode(), adminLogin.getInt(Code));
|
PageData<ArticleModel> pageData = response.getResult();
|
||||||
assertNotNull(adminLogin.getString(Result));
|
assertNotEquals(0, pageData.getTotal());
|
||||||
// 判断pageInfo是否包装完全
|
assertEquals(1, pageData.getPageNum());
|
||||||
PageData<ArticleModel> pageData = (PageData<ArticleModel>) JSONObject.toBean(adminLogin.getJSONObject(Result), PageData.class);
|
assertEquals(10, pageData.getPageSize());
|
||||||
assertNotEquals(0, pageData.getTotal());
|
// 内容完整
|
||||||
assertEquals(1, pageData.getPageNum());
|
for (ArticleModel a : pageData.getList()) {
|
||||||
assertEquals(10, pageData.getPageSize());
|
assertNotNull(a.getTitle());
|
||||||
// 内容完整
|
assertNotNull(a.getId());
|
||||||
for (Object arc : pageData.getList()) {
|
assertNotNull(a.getOriginal());
|
||||||
ArticleModel a = (ArticleModel) JSONObject.toBean(JSONObject.fromObject(arc), ArticleModel.class);
|
assertNotNull(a.getPublishDateFormat());
|
||||||
assertNotNull(a.getTitle());
|
assertNotNull(a.getOpen());
|
||||||
assertNotNull(a.getId());
|
assertNotNull(a.getReadingNumber());
|
||||||
assertNotNull(a.getOriginal());
|
assertNotNull(a.getLikeCount());
|
||||||
assertNotNull(a.getPublishDateFormat());
|
assertNotNull(a.getDislikeCount());
|
||||||
assertNotNull(a.getOpen());
|
assertNull(a.getMdContent());
|
||||||
assertNotNull(a.getReadingNumber());
|
}
|
||||||
assertNotNull(a.getLikeCount());
|
});
|
||||||
assertNotNull(a.getDislikeCount());
|
|
||||||
assertNull(a.getMdContent());
|
|
||||||
}
|
|
||||||
});
|
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user