使⽤Mock测试controller层
package action;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import st.context.ContextConfiguration;
import st.context.junit4.SpringJUnit4ClassRunner;
import st.context.web.WebAppConfiguration;
import st.web.servlet.MockMvc;
import st.web.servlet.MvcResult;
import st.web.servlet.ResultActions;
import st.quest.MockMvcRequestBuilders;
import st.sult.MockMvcResultMatchers;
import st.web.servlet.setup.MockMvcBuilders;
import org.t.WebApplicationContext;
/**
* Created by duanxinli on 2019/10/16.
*/
@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration()
@ContextConfiguration({"l","l","l"}) public class MVCTest {
@Autowired
private WebApplicationContext wac;
private MockMvc mockMvc;
@Before
public void init(){
}
@Test
public void getshortCutTest() throws Exception {
ResultActions kMvc.("/shortcut/listAll")
.accept(MediaType.APPLICATION_JSON));//返回值接收json
reaction.andExpect(MockMvcResultMatchers.status().isOk());
MvcResult mvcResult =reaction.andReturn();
System.out.Response().getContentAsString());
}
@Test
public void postshortCutTest() throws Exception {
ResultActions reaction =kMvc.perform(MockMvcRequestBuilders.post("/policy/info/save")
.contentType(MediaType.APPLICATION_JSON)//请求体时json
.header("Timestamp", "1496656373791")
.header("AppId", "1003"));
reaction.andExpect(MockMvcResultMatchers.status().isOk());
spring framework面试题MvcResult mvcResult =reaction.andReturn();
System.out.Response().getContentAsString());
}
}
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论