抽离json -> Response 对象的方法
This commit is contained in:
@@ -10,6 +10,8 @@ import org.junit.After;
|
|||||||
import org.junit.Before;
|
import org.junit.Before;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.junit.runner.RunWith;
|
import org.junit.runner.RunWith;
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
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.http.MediaType;
|
||||||
@@ -18,13 +20,15 @@ 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.ResultActions;
|
||||||
import org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder;
|
import org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder;
|
||||||
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
|
|
||||||
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 java.io.IOException;
|
||||||
|
import java.io.UnsupportedEncodingException;
|
||||||
import java.util.UUID;
|
import java.util.UUID;
|
||||||
|
|
||||||
import static cn.celess.blog.enmu.ResponseEnum.SUCCESS;
|
import static cn.celess.blog.enmu.ResponseEnum.SUCCESS;
|
||||||
@@ -44,6 +48,8 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.
|
|||||||
@ActiveProfiles("test")
|
@ActiveProfiles("test")
|
||||||
public class BaseTest {
|
public class BaseTest {
|
||||||
|
|
||||||
|
private final Logger logger = LoggerFactory.getLogger(this.getClass());
|
||||||
|
|
||||||
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";
|
||||||
@@ -120,8 +126,10 @@ public class BaseTest {
|
|||||||
assertNotNull(token);
|
assertNotNull(token);
|
||||||
return token;
|
return token;
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
|
logger.error("测试登录错误");
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
|
assertNotNull(str);
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -134,6 +142,7 @@ public class BaseTest {
|
|||||||
try {
|
try {
|
||||||
// 测试getMockData方法
|
// 测试getMockData方法
|
||||||
assertNotNull(getMockData(get("/headerInfo")));
|
assertNotNull(getMockData(get("/headerInfo")));
|
||||||
|
getMockData((get("/headerInfo"))).andDo(result -> assertNotNull(getResponse(result)));
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
@@ -200,4 +209,27 @@ public class BaseTest {
|
|||||||
}
|
}
|
||||||
return mockMvc.perform(builder).andExpect(status().isOk());
|
return mockMvc.perform(builder).andExpect(status().isOk());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected <T> Response<T> getResponse(String json) {
|
||||||
|
Response<T> response = null;
|
||||||
|
try {
|
||||||
|
response = mapper.readValue(json, new TypeReference<Response<T>>() {
|
||||||
|
});
|
||||||
|
} catch (IOException e) {
|
||||||
|
logger.error("接续json Response对象错误,json:[{}]", json);
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
assertNotNull(response);
|
||||||
|
return response;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected <T> Response<T> getResponse(MvcResult result) {
|
||||||
|
try {
|
||||||
|
return getResponse(result.getResponse().getContentAsString());
|
||||||
|
} catch (UnsupportedEncodingException e) {
|
||||||
|
logger.error("接续json Response对象错误,result:[{}]", result);
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user