Spring+Strutsでテスト。
SpringにインテグレーションしたStrutsでテストしてみたという話。
Appfuseをサンプルにやったら、30分ほどで完成。
コードはこんな感じ
... public class UserCreateActionTest extends MockStrutsTestCase { private WebApplicationContext ctx = null; public UserCreateActionTest(String name) { super(name); } protected void setUp() throws Exception { super.setUp(); MockServletContext sc = new MockServletContext(""); sc.addInitParameter(ContextLoader.CONFIG_LOCATION_PARAM, "Bean定義ファイルのパス"); ServletContextListener contextListener = new ContextLoaderListener(); ServletContextEvent event = new ServletContextEvent(sc); contextListener.contextInitialized(event); getSession().getServletContext().setAttribute( WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, sc.getAttribute( WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE)); this.ctx = WebApplicationContextUtils.getRequiredWebApplicationContext( getSession().getServletContext()); } protected void tearDown() throws Exception { super.tearDown(); this.ctx = null; } public void testExecute() { ... } }
唯一ハマったのは、サービス層でTransactionManager使ってて、StrutsTestCaseに「org.aopalliance.aop.Advice ねぇよ」って怒られたくらい。普段はいらないのに。まぁ、どっかでContextを初期化するときに必要なんだろうなと。
プレゼンテーション/サービス/パーシステンスだと、サービス層のテストが一番めんどいなぁ。モック作ったり。jMockはカンタンだけど、ロジック複雑になるとカバレッジ的に良くないような。どうなんだろ。