繁体   English   中英

在Spring MockMVC测试中,如何链接几个网页的访问?

[英]In Spring MockMVC tests, how to chain visit of several webpages?

我有一个需要登录的Web应用程序。 登录成功后,将加载许多会话属性,其他网页的后续导航将需要这些属性。

我正在使用MockMVC使用Spring测试框架4.12测试此Web应用程序。

登录页面访问后如何链接第二个页面访问操作? 就像是:

mockMvc.perform(post("/login").session(session).param("username", "Jack").param("password","Jack'sPassword"))
               .perform(get("/anotherPage")).andExpect(/*some session attribute are successfully loaded*/)

您可以使用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*
       )

在5.0.0版中,您可以配置MockMvc以保留执行调用之间的会话。

    mvc = MockMvcBuilders
    .webAppContextSetup(context)
    .apply(sharedHttpSession())
    .build();

参见: 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM