1
1

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.

JUnit 5: テストケースをenumで記述する方法

Last updated at Posted at 2019-08-10

JUnit 5では**@ParameterizedTestアノテーションが用意されていて、様々な引数ソースから得られる値をテストメソッドに引数として渡すことができます。この記事では、こうした引数ソースの一つである@EnumSource**を活用して、多くのテストケースを手軽に記述する方法を紹介したいと思います。

準備

**@ParameterizedTest**を使用する場合は、プロジェクトの依存ファイルとしてjunit-jupiter-engineに加えてjunit-jupiter-paramsも明示的に追加する必要があります。また、今回のサンプルコードでは、テスト結果の検証にAssertJを使いました。

pom.xml

<dependency>
    <groupId>org.junit.jupiter</groupId>
    <artifactId>junit-jupiter-engine</artifactId>
    <version>5.5.1</version>
    <scope>test</scope>
</dependency>
<dependency>
    <groupId>org.junit.jupiter</groupId>
    <artifactId>junit-jupiter-params</artifactId>
    <version>5.5.1</version>
    <scope>test</scope>
</dependency>
<dependency>
    <groupId>org.assertj</groupId>
    <artifactId>assertj-core</artifactId>
    <version>3.13.1</version>
    <scope>test</scope>
</dependency>

テストの対象

ここでは、JDKのBigInteger#gcd()メソッドをテストの対象とします。このメソッドは、自身と与えられた引数の最大公約数を返します。

テストケースを書く

入力値2つと正しい答えの組み合わせをenum型の定数としてひたすら記述しましょう。

enum GcdTestCase {
    THIS_IS_BIGGER(25, 10, 5),
    OTHER_IS_BIGGER(14, 21, 7),
    BOTH_ARE_EVEN(12, 8, 4),
    BOTH_ARE_ODD(27, 45, 9),
    BOTH_ARE_ZERO(0, 0, 0),
    BOTH_ARE_ONE(1, 1, 1),
    THIS_IS_ZERO(0, 3, 3),
    OTHER_IS_ZERO(4, 0, 4),
    THIS_IS_DIVISOR(3, 12, 3),
    OTHER_IS_DIVISOR(20, 4, 4),
    NO_COMMON_DIVISOR(7, 9, 1);

    final BigInteger a;
    final BigInteger b;
    final BigInteger expected;

    GcdTestCase(long a, long b, long expected) {
        this.a = BigInteger.valueOf(a);
        this.b = BigInteger.valueOf(b);
        this.expected = BigInteger.valueOf(expected);
    }
}

とりあえず11個のテストケースを作成しましたが、後からテストケースを追加するのは比較的容易ですよね。列挙子を増やすだけです。

テストメソッドを書く

テストメソッドには**@ParameterizedTestアノテーションを指定しますが、同時に@EnumSource**アノテーションを使って上で作成したenum型を指定します。

@ParameterizedTest
@EnumSource(GcdTestCase.class)
public void gcdShouldReturnGreatestCommonDivisorAsExpected(GcdTestCase test) {
    BigInteger actual = test.a.gcd(test.b);
    assertThat(actual).isEqualTo(test.expected);
}

テストを実行する

作成したテストをmvn testで実行すると、当然ですが、すべてのテストが成功します。

[INFO] -------------------------------------------------------
[INFO]  T E S T S
[INFO] -------------------------------------------------------
[INFO] Running org.example.junit5.tips.BigIntegerTest
[INFO] Tests run: 11, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.156 s - in org.example.junit5.tips.BigIntegerTest
[INFO]
[INFO] Results:
[INFO]
[INFO] Tests run: 11, Failures: 0, Errors: 0, Skipped: 0

まとめ

今回はJUnit 5の**@EnumSource**を利用して多くのテストケースを手軽に記述する方法を紹介しました。
サンプルコードはGitHubに上げてあります。ライセンスはThe Unlicenseです。

1
1
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
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?