日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

测试Spring的“会话”范围

發布時間:2023/12/3 javascript 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 测试Spring的“会话”范围 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

在基于Spring的Web應用程序中,bean的作用域可以是用戶“會話”。 從本質上講,這意味著對會話范圍的Bean的狀態更改僅在用戶會話范圍內可見。

此項的目的是簡單地突出顯示Spring Test MVC提供的一種方法,以測試將會話范圍的bean作為依賴項的組件。

考慮一下UserPreferences類的Spring參考文檔中的示例,其中包含用戶的timeZoneId:

@Component @Scope(value="session", proxyMode=ScopedProxyMode.TARGET_CLASS) public class UserPreferences {private String timeZoneId="default";public String getTimeZoneId() {return timeZoneId;}public void setTimeZoneId(String timeZoneId) {this.timeZoneId = timeZoneId;} }

在這里,范圍被標記為“會話”,并且proxyMode被明確指定為TARGET_CLASS,以指示Spring創建CGLIB代理(因為UserPreferences不實現任何其他接口)。

現在考慮使用此會話范圍的bean作為依賴項的控制器:

@Controller public class HomeController {@Autowired private UserPreferences userPreferences;@RequestMapping(value="/setuserprefs")public String setUserPrefs(@RequestParam("timeZoneId") String timeZoneId, Model model) {userPreferences.setTimeZoneId(timeZoneId);model.addAttribute("timeZone", userPreferences.getTimeZoneId());return "preferences";}@RequestMapping(value="/gotopage")public String goToPage(@RequestParam("page") String page, Model model) {model.addAttribute("timeZone", userPreferences.getTimeZoneId());return page;} }

這里有兩種控制器方法,在第一種方法中,設置用戶偏好,在第二種方法中,讀取用戶偏好。 如果會話作用域的bean運行正常,則在用戶會話中對“ / setuserprefs”的調用應在UserPreferences bean中設置timeZoneId首選項,而在同一會話中的另一個調用“ / gotopage”應成功檢索先前設置的首選項。

使用現在與Spring-test模塊打包在一起的Spring MVC測試支持,對此進行測試很簡單。

測試看起來像這樣:

首先使用Spring Java Configuration進行測試的bean定義:

@Configuration @EnableWebMvc @ComponentScan({"scope.model","scope.services", "scope.web"}) public class ScopeConfiguration {}

和測試:

import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.mock.web.MockHttpSession; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import org.springframework.test.context.web.WebAppConfiguration; import org.springframework.test.web.servlet.MockMvc; import org.springframework.web.context.WebApplicationContext;import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*; import static org.springframework.test.web.servlet.setup.MockMvcBuilders.*;@RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(classes=ScopeConfiguration.class) @WebAppConfiguration public class ScopeConfigurationTest {@Autowiredprivate WebApplicationContext wac;private MockMvc mockMvc;@Beforepublic void setup() {this.mockMvc = webAppContextSetup(this.wac).build();}@Testpublic void testSessionScope() throws Exception {MockHttpSession mocksession = new MockHttpSession();this.mockMvc.perform(get("/setuserprefs?timeZoneId={timeZoneId}", "US/Pacific").session(mocksession)).andExpect(model().attribute("timeZone", "US/Pacific"));this.mockMvc.perform(get("/gotopage?page={page}", "home").session(mocksession)).andExpect(model().attribute("timeZone", "US/Pacific"));this.mockMvc.perform(get("/gotopage?page={page}", "home").session(new MockHttpSession())).andExpect(model().attribute("timeZone", "default"));} }

在測試中,首先創建一個MockHttpSession來模擬用戶會話。 隨后的兩個請求是在此模擬會話的上下文中發出的,因此期望在測試中聲明的控制器中可以看到相同的UserPreferences bean。 在第三個請求中,創建了一個新會話,這次是在控制器中看到另一個UserPreferences bean,這是通過查找其他屬性來斷言的。

這演示了使用Spring測試MVC支持來測試會話范圍的bean的干凈方法。

參考: all和其他博客中的JCG合作伙伴 Biju Kunjummen 測試了Spring的“會話”范圍 。

翻譯自: https://www.javacodegeeks.com/2013/06/testing-spring-session-scope.html

總結

以上是生活随笔為你收集整理的测试Spring的“会话”范围的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。