抽离Mock请求的方法,过渡到jackson
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
package cn.celess.blog;
|
||||
|
||||
|
||||
import cn.celess.blog.entity.request.LoginReq;
|
||||
import net.sf.json.JSONObject;
|
||||
import org.junit.After;
|
||||
@@ -8,23 +9,25 @@ 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.mock.web.MockHttpSession;
|
||||
import org.springframework.test.context.ActiveProfiles;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
import org.springframework.test.context.web.WebAppConfiguration;
|
||||
import org.springframework.test.web.servlet.MockMvc;
|
||||
import org.springframework.test.web.servlet.MvcResult;
|
||||
import org.springframework.test.web.servlet.ResultHandler;
|
||||
import org.springframework.test.web.servlet.ResultActions;
|
||||
import org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder;
|
||||
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.web.context.WebApplicationContext;
|
||||
|
||||
import javax.servlet.http.Cookie;
|
||||
|
||||
import java.util.UUID;
|
||||
|
||||
import static cn.celess.blog.enmu.ResponseEnum.SUCCESS;
|
||||
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: 小海
|
||||
@@ -40,6 +43,8 @@ public class BaseTest {
|
||||
protected MockMvc mockMvc;
|
||||
protected final static String Code = "code";
|
||||
protected final static String Result = "result";
|
||||
private static String userToken = null;
|
||||
private static String adminToken = null;
|
||||
|
||||
@Autowired
|
||||
private WebApplicationContext wac;
|
||||
@@ -59,6 +64,7 @@ public class BaseTest {
|
||||
|
||||
|
||||
protected String adminLogin() {
|
||||
if (adminToken != null) return adminToken;
|
||||
try {
|
||||
LoginReq req = new LoginReq();
|
||||
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"))
|
||||
// .andDo(MockMvcResultHandlers.print())
|
||||
.andReturn().getResponse().getContentAsString();
|
||||
String token = JSONObject.fromObject(str).getJSONObject(Result).getString("token");
|
||||
assertNotNull(token);
|
||||
return token;
|
||||
adminToken = JSONObject.fromObject(str).getJSONObject(Result).getString("token");
|
||||
assertNotNull(adminToken);
|
||||
return adminToken;
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
return null;
|
||||
@@ -78,6 +84,7 @@ public class BaseTest {
|
||||
}
|
||||
|
||||
protected String userLogin() {
|
||||
if (userToken != null) return userToken;
|
||||
try {
|
||||
LoginReq req = new LoginReq();
|
||||
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"))
|
||||
// .andDo(MockMvcResultHandlers.print())
|
||||
.andReturn().getResponse().getContentAsString();
|
||||
String token = JSONObject.fromObject(str).getJSONObject(Result).getString("token");
|
||||
assertNotNull(token);
|
||||
return token;
|
||||
userToken = JSONObject.fromObject(str).getJSONObject(Result).getString("token");
|
||||
assertNotNull(userToken);
|
||||
return userToken;
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
return null;
|
||||
@@ -104,4 +111,24 @@ public class BaseTest {
|
||||
protected String randomStr(int 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());
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user