簡體   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