2
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Junit4でのテスト順序を列挙型で制御する

Last updated at Posted at 2018-08-21

概要

JUnitでは@Testアノテーションで定義されたメソッドの実行順序は保証されていません。

そこで下記の3つのクラスを作成し、実行順序を制御します。

  • OrderedRunner
    • テストクラスに設定するアノテーションクラス
  • Order
    • テストメソッドに設定するアノテーションクラス
  • TestMethod
    • Orderクラスの設定値の列挙した列挙型クラス

処理の流れとしてテスト実行時にOrderedRunnerが各テストメソッドに付与された
Orderの設定値を読み通り、実行順序を制御する流れになります。

TestMethod.java

public enum TestMethod {
    strArrayIsNullTest,
    strIsNullTest
};
Order.java

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;

@Retention(RetentionPolicy.RUNTIME)
public @interface Order {
    TestMethod order();
}

OrderedRunner.java
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import org.junit.runners.BlockJUnit4ClassRunner;
import org.junit.runners.model.FrameworkMethod;
import org.junit.runners.model.InitializationError;

public class OrderedRunner extends BlockJUnit4ClassRunner {
    public OrderedRunner(Class<?> klass) throws InitializationError  {
        super(klass);
    }

    @Override
    protected List<FrameworkMethod> computeTestMethods() {
        List<FrameworkMethod> list = super.computeTestMethods();
        Collections.sort(list, new Comparator<FrameworkMethod>() {
            @Override
            public int compare(FrameworkMethod f1, FrameworkMethod f2) {
                Order o1 = f1.getAnnotation(Order.class);
                Order o2 = f2.getAnnotation(Order.class);

                if (o1 == null || o2 == null){
                    return -1;
                }
                return o1.order().ordinal() - o2.order().ordinal();
            }
        });
        return list;
    }
}

実践

Foo.java
public class Foo {
    public static boolean isNull(String[] arg) {
        return arg == null;
    }

    public static boolean isNull(String str) {
        return str == null || str.length() == 0 || str.equals("");
    }
}

FooTest.java
import static org.junit.Assert.*;

import org.junit.Test;
import org.junit.runner.RunWith;

@RunWith(OrderedRunner.class)
public class FooTest {

    @Test
    @Order(order=TestMethod.strArrayIsNullTest)
    public void strArrayIsNullTest() {
        String[] arg = {"test"};
        assertFalse(Foo.isNull(arg));
        System.out.println("strArrayIsNullTest OK");
    }

    // テストを追加しても、TestMethodに要素を追加すれば順序性が担保される。
    /**
     * @Test
     * @Order(order=TestMethod.reservationsTest)
     * public void reservationsTest();
     */

    @Test
    @Order(order=TestMethod.strIsNullTest)
    public void strIsNullTest() {
        String str = "test";
        assertFalse(Foo.isNull(str));
        System.out.println("strIsNullTest OK");
    }
}

参考資料

2
3
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
2
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?