javascript
测试Spring的“会话”范围
在基于Spring的Web應用程序中,bean的作用域可以是用戶“會話”。 從本質上講,這意味著對會話范圍的Bean的狀態更改僅在用戶會話范圍內可見。
此項的目的是簡單地突出顯示Spring Test MVC提供的一種方法,以測試將會話范圍的bean作為依賴項的組件。
考慮一下UserPreferences類的Spring參考文檔中的示例,其中包含用戶的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的干凈方法。
翻譯自: https://www.javacodegeeks.com/2013/06/testing-spring-session-scope.html
總結
以上是生活随笔為你收集整理的测试Spring的“会话”范围的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 华为电脑翻译快捷键(华为手机快捷翻译功能
- 下一篇: 电脑程序内存清理方法(如何清理电脑系统的