[英]In Spring MockMVC tests, how to chain visit of several webpages?
I have a web application that requires Login. 我有一个需要登录的Web应用程序。 Upon login success, many session attributes are loaded, which will be needed by subsequent navigation of other web pages.
登录成功后,将加载许多会话属性,其他网页的后续导航将需要这些属性。
I am testing this web application using Spring test framework 4.12, using MockMVC. 我正在使用MockMVC使用Spring测试框架4.12测试此Web应用程序。
How do I chain a second page visit action after the login page visit? 登录页面访问后如何链接第二个页面访问操作? Something like:
就像是:
mockMvc.perform(post("/login").session(session).param("username", "Jack").param("password","Jack'sPassword"))
.perform(get("/anotherPage")).andExpect(/*some session attribute are successfully loaded*/)
You can use andDo method to chain your requests 您可以使用andDo方法来链接您的请求
mockMvc.perform(post("/login").session(session)
.param("username","Jack").param("password","Jack'sPassword"))
//Expectations on the first request
.andExpect(status().ok())
//Then chain the request
.andDo(
result -> mockMvc.perform(get("/anotherPage")).andExpect(/*some session attribute are successfully loaded*
)
In version 5.0.0 you can configure MockMvc to preserve the session between performing calls. 在5.0.0版中,您可以配置MockMvc以保留执行调用之间的会话。
mvc = MockMvcBuilders
.webAppContextSetup(context)
.apply(sharedHttpSession())
.build();
see: https://docs.spring.io/spring/docs/current/spring-framework-reference/testing.html#spring-mvc-test-server-setup-steps 参见: https : //docs.spring.io/spring/docs/current/spring-framework-reference/testing.html#spring-mvc-test-server-setup-steps
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.