- 追加された行はこの色です。
- 削除された行はこの色です。
[[FrontPage]]
#contents
2008/03/13からのアクセス回数 &counter;
* Spring-MVCでspringのsession scopeオブジェクトをテストしたい [#caea628a]
Spring 2.0から導入されたsession scopeオブジェクトは、DispatcherServletで処理されるため
MockHttpServletRequestを使った単体テストでは、bean定義ファイルでscope="session"で
定義されたsession scopeオブジェクトのsession単位での持ち回りのテストができません。
** session scopeオブジェクトに必要なもの [#z1d0df69]
session scopeオブジェクトのテストには、
-- GenericWebApplicationContextをcreateApplicationContextメソッドで返す
-- WebApplicationContextのbeanFactoryにSCOPE_REQUEST, SCOPE_SESSION, SCOPE_GBLOBAL_SESSIONスコープを登録する
-- HTTPリクエストを処理する前に、WebApplicationContextにサーブレットコンテキストをセットし、サーブレットのROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTEにWebApplicationContextをセットする
-- HTTPリクエストの処理の後に、RequestContextFilterと同じ処理を追加する
*** AbstractTransactionalSpringWebContextTestsを追加 [#h0cadda5]
GenericWebApplicationContextをcreateApplicationContextメソッドで返すTestCaseとして
AbstractTransactionalSpringWebContextTestsを追加しました。
AbstractTransactionalSpringWebContextTestsのcreateApplicationContextは以下の通りです。
#pre{{
protected ConfigurableApplicationContext createApplicationContext(final String[] locations) {
GenericWebApplicationContext context =
new GenericWebApplicationContext(
new GenericApplicationContext().getDefaultListableBeanFactory());
context.setServletContext(new MockServletContext());
customizeBeanFactory(context.getDefaultListableBeanFactory());
createBeanDefinitionReader(context).loadBeanDefinitions(locations);
context.refresh();
// StaticWebApplicationContext のpostProcessBeanFactoryと同じ処理を追加
ConfigurableListableBeanFactory beanFactory = context.getBeanFactory();
beanFactory.registerScope(WebApplicationContext.SCOPE_REQUEST, new RequestScope());
beanFactory.registerScope(WebApplicationContext.SCOPE_SESSION, new SessionScope(false));
beanFactory.registerScope(WebApplicationContext.SCOPE_GLOBAL_SESSION,
new SessionScope(true));
return context;
}
}}
*** beginRequest, endRequestメソッドの追加 [#l7d22f78]
HTTPリクエストの前後にRequestContextFilterと同様の処理行うメソッド、beginRequest, endRequestメソッドをAbstractTransactionalSpringWebContextTestsに定義しました。
beginRequestメソッドは、
#pre{{
public void beginRequest(MockHttpServletRequest req) {
ServletRequestAttributes attributes = new ServletRequestAttributes(req);
req.setAttribute(REQUEST_ATTRIBUTES_ATTRIBUTE, attributes);
RequestContextHolder.setRequestAttributes(attributes);
ServletContext sc = req.getSession().getServletContext();
GenericWebApplicationContext wac = (GenericWebApplicationContext)getApplicationContext();
wac.setServletContext(sc);
sc.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, (GenericWebApplicationContext)getApplicationContext());
}
}}
endRequestメソッドは、
#pre{{
public void endRequest(MockHttpServletRequest req) {
ServletRequestAttributes attributes = (ServletRequestAttributes) req
.getAttribute(REQUEST_ATTRIBUTES_ATTRIBUTE);
ServletRequestAttributes threadAttributes = (ServletRequestAttributes) RequestContextHolder
.getRequestAttributes();
if (threadAttributes != null) {
// We're assumably within the original request thread...
if (attributes == null) {
attributes = threadAttributes;
}
RequestContextHolder.setRequestAttributes(null);
LocaleContextHolder.setLocale(null);
}
if (attributes != null) {
attributes.requestCompleted();
if (logger.isDebugEnabled()) {
logger.debug("Cleared thread-bound request context: " + req);
}
}
}
}}
と定義しました。
** テストメソッドの記述方法 [#r94c1f00]
session scopeオブジェクトのテストメソッドを定義するには、
-- AbstractTransactionalSpringWebContextTestsのサブクラスとしてTestCaseを定義する
-- HTTP要求を処理するhadleRequestの前後をbeginRequest, endRequestで挟む
-- 同一セッション内の連続する処理をテストする場合には、直前のsessionをMockHttpServletRequestにセットする
*** HTTP要求の前後処理例 [#a392de6c]
以下にMemberOpsControllerを例にHTTP要求の前後処理の仕方を示します。
#pre{{
MemberOpsController memberOpsController =
(MemberOpsController)getApplicationContext().getBean("memberOpsController");
ModelAndView mv = null;
beginRequest(req);
mv = memberOpsController.handleRequest(req,new MockHttpServletResponse());
endRequest(req);
}}
*** セッション情報の持ち回り [#h311f391]
同一セッションは、つぎのようにして持ち回ることができます。
#pre{{
HttpSession session = req.getSession();
req = new MockHttpServletRequest("POST","memberops/list.htm");
req.setSession(session);
}}
これで、session scopeオブジェクトの単体テストができるようになります。
** コメント [#y379a8a1]
この記事は、
#vote(おもしろかった[0],そうでもない[0],わかりずらい[1])
#vote(おもしろかった[0],そうでもない[0],わかりずらい[2])
皆様のご意見、ご希望をお待ちしております。
#comment_kcaptcha