SAStrutsを勉強してみる (7) - LoginInterceptorをテスト -

2012-10-08T00:00:00+00:00 Java SAStruts

タイトルなんですが、S2AOPな模様げなのをテストする(正式な)手段が結局分からないので、とりあえずMethodInvocationぶちこんじゃえば良いんじゃね的な感じでやってみた

InterceptorTestCase.java

import java.lang.reflect.AccessibleObject;
import java.lang.reflect.Method;

import org.aopalliance.intercept.MethodInvocation;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.seasar.framework.unit.Seasar2;

import sample.action.dashboard.IndexAction;
import sample.dto.UserDto;
import sample.interceptor.LoginInterceptor;

import static org.junit.Assert.*;

@RunWith(Seasar2.class)
public class InterceptorTestCase {

    private LoginInterceptor interceptor;

    private UserDto userDto;

    @Test
    public void testInterceptor() {
        MethodInvocation inv = new MethodInvocation() {
            @Override
            public Object proceed() throws Throwable {
                return ((IndexAction)getThis()).index();
            }

            @Override
            public Object getThis() {
                return new IndexAction();
            }

            @Override
            public AccessibleObject getStaticPart() {
                return null;
            }

            @Override
            public Object[] getArguments() {
                return null;
            }

            @Override
            public Method getMethod() {
                Method m = null;

                try {
                    m = IndexAction.class.getMethod("index");
                } catch (SecurityException e) {
                    e.printStackTrace();
                } catch (NoSuchMethodException e) {
                    e.printStackTrace();
                }

                return m;
            }
        };

        userDto.userName = "dummy";

        try {
            assertSame(interceptor.invoke(inv), "index.jsp");
        } catch (Throwable e) {
            e.printStackTrace();
        }
    }
}

テスト上で(Mock)HttpSessionを注入させて、チェックするのもするべきだとは思いますが(今回はそのテスト部のコードは削除)

っていう感じですかね。正式的にどうやるのが良いのかは微妙ですけど

ConnectivityManager.isActiveNetworkMeteredな件 SAStrutsを勉強してみる (6) - LoginInterceptor -