JMockitでSeasar2のInterceptorのテストケースを書く。
テストフレームワークにJMockit、WebフレームワークにSeasar2を使用した場合に、Interceptorのテストケースを書いたので、忘れないように書いておく。
TestInterceptor
第一引数に文字列が渡されたら、「test」という文字列に変換するインターセプタ。
public class TestInterceptor implements MethodInterceptor { public Object invoke(MethodInvocation invocation) throws Throwable { Object[] args = invocation.getArguments(); if(args.length == 0) { return invocation.proceed(); } Object obj = args[0]; if(obj instanceof String) { String str = (String) obj; str = "test"; } return invocation.proceed(); } }
TestInterceptorTest
JMockitを使って、上記インターセプタのテストケースを書く。
public class TestInterceptorTest { /** テスト対象 */ private final TestInterceptor target = new TestInterceptor(); private String testStr = ""; /** MethodInvocationモック */ @Mocked private MethodInvocation invocation; @Test public final void testInvoke() { new Expectations() { { invocation.getArguments(); result = new Object[] {testStr }; invocation.proceed(); result = "test"; } }; target.invoke(invocation); assertEquals("test", testStr); } }
こんな感じでよいのか。。
まだまだ勉強中。。