LoginSignup
5
6

More than 5 years have passed since last update.

Spring AOP の 単体テストを行う方法

Posted at

やりたいこと

Spring AOPで作った業務横断的な処理を持ったクラスを単体でテストしたい。

Spring AOP の仕組み(ざっくり)と単体テスト時の問題点

Spring AOPはDIを基に成立っており、コンポーネントを使う側がproxy(コンテナに登録したBeanそのものではなく、そのBeanに対しAOPで定義した機能を拡張(enhance)させた)インスタンスをインジェクトすることにより、AOPを実現します。
aop.png

しかしながら、今回やりたいのは単体テストなので、DIコンテナを起動させず、コンテキストに依存しないでテストをする方法を用意したいと思います。

今回用意したサンプル

引数にnullがあればNullPointerExceptionを投げる処理をAOPで実装して、TestService(テスト用クラス)クラスのメソッドに適用させます。(NullCheckアノテーションは単なるマーカーなのでここでは省略します。)

AspectLogic.java

@Aspect
@Component
public class AspectLogic {
    @Around("@annotation(NullCheck)")
    public Object invoke(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
        System.out.println("NullCheck!!");
        Stream.of(proceedingJoinPoint.getArgs()).forEach(Objects::requireNonNull);//全引数を取得してObejcts::requireNonNullでチェック
        return proceedingJoinPoint.proceed();
    }
}

TestService.java

public class TestService {
    @NullCheck
    public void doSomething(Object arg) {
    }
}

AspectJProxyFactoryからproxyを取得する。

Spring DIがBeanからproxyを取得する部分を自前で実装しましょう。AspectJProxyFactoryのインスタンス
に今回作成したAspectなクラスをaddします。
ファクトリから取得したproxyを使ってテストサンプルメソッドを叩くと、AOPで定義した処理が呼ばれ、NullPointerExceptionが投げられていることが確認できたかと思われます。

TestAspectLogic.java
public class TestAspectLogic {
    @Test
    public void testAspect() {

        AspectJProxyFactory factory = new AspectJProxyFactory(new TestService());
        factory.addAspect(new AspectLogic()); //ここでAspectLogicクラスを適用
        TestService proxy = factory.getProxy();

        try {
            proxy.doSomething(null);
            failBecauseExceptionWasNotThrown(NullPointerException.class);
        } catch(NullPointerException npe) {
        }

    }
}

出力
NullCheck!!

まとめ

AOPもしっかりテストを書きましょう。

5
6
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
6