MockMvc 怎么集成测试?
@Beforepublic void initTests() throws Exception {
mvc = MockMvcBuilders.webAppContextSetup(context)
.addFilters(springSecurityFilterChain)
.build();
}
@Test
public void testLogin() throws Exception {
mvc.perform(post("/login").param("username", "15812345678").param("password", "123456"))
.andDo(print())
.andExpect(MockMvcResultMatchers.redirectedUrl("/api/user/list"));
}流程是:
[*]先登陆
[*]取得用户数据
现在问题是redirectedUrl的action ,我要加入参数进去,才能完成/api/user/list的请求。但是一直都302,说明登陆没通过,求救大神帮帮解答解答呢?今天解决了。思路时 在第二个请求中放入session 。这样就不会出现被登陆请求拦截问题了。到此位置,mockmvc ,集成完一个业务流程的测试,爽啊
代码如下@Before
public void initTests() throws Exception {
mvc = MockMvcBuilders.webAppContextSetup(context).addFilters(springSecurityFilterChain).build();
session = (MockHttpSession) mvc.perform(post("/login")
.param("username", "15812345678")
.param("password","e10adc3949ba59abbe56e057f20f883e"))
.andReturn().getRequest().getSession();;
}
@Test
public void testDownSummary() throws Exception {
List<LoanProgress> loanProgress = new ArrayList<>();
LoanProgress l1 = new LoanProgress();
l1.setApplyNo("RZ000001");
l1.setRateHighest(0.12);
LoanProgress l2 = new LoanProgress();
l2.setApplyNo("RZ000002");
l2.setRateHighest(0.12);
loanProgress.add(l1);
loanProgress.add(l2);
String jsonArray = JSONObject.toJSONString(loanProgress);
logger.debug(jsonArray);
mvc.perform(get("/api/loanProgress/accept")
.session(session)
.contentType(MediaType.APPLICATION_JSON)
.content(jsonArray))
.andExpect(status().isOk())
.andDo(print());
}
页:
[1]