LoginSignup
1
0

More than 3 years have passed since last update.

テストアノテーションとアサートステートメントについて(JUnit5)

Posted at

目次

・アノテーションとは
・アサートとは
・実際にコードを書いてみよう

アノテーションとは

  • アノテーションは、コードの読みやすさを向上させるために
    Javaソースコードに追加することができる構文メタデータの特別な形式です。

  • これらのアノテーションは、テストメソッドの前後に実行されるメソッドに関する情報を提供します。

  • すべてのメソッドが完了する前と後に実行されるメソッド、テスト実行中に無視されるメソッドまたはクラス。

image.png

アサートとは

image.png

  • アサートとは、テストケースの合否判定に用いられる手法です。
  • J-Unit では、すべてのassertionsは Assert クラスにあります。

アサーションの使い方(assertステートメントの書き方)

  • Javaでのアサーションは、「assert」ステートメントを使って表現します。

実際にコードを書いてみよう

  • Assertクラスを作成

image.png

image.png

Assert.java
package co.hoge;

import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNotSame;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertSame;
import static org.junit.jupiter.api.Assertions.assertTrue;

import org.junit.Test;

public class Assert {
    @Test
    public void testAssertions() {
        String str = new String("hoge");
        String str1 = new String("hoge");
        String str2 = null;
        String str3 = "hoge";
        String str4 = "hoge";

        int val = 5;
        int val1 = 6;
        String[] exceptedArray = { "one", "two", "three" };
        String[] resultArray = { "one", "two", "three" };

        assertEquals(str, str1);

        // trueのチェック
        assertTrue(val < val1);

        // falseのチェック
        assertFalse(val > val1);

        // nullのチェック 
        assertNotNull(str);

        // もしnullの場合のチェック
        assertNull(str2);

        // 参照先が同じことをチェック
        assertSame(str3, str4);

        // 参照先が違うことをチェック
        assertNotSame(str, str1);

        // 同じ配列かチェック
        assertArrayEquals(exceptedArray, resultArray);
    }
}

✅画像を同じ結果になることを確認

image.png

作成したテストを実行するためのmainメソッドを作成

  • MainClassクラスを作成

image.png

MainClass.java
package co.hoge;

import org.junit.runner.JUnitCore;
import org.junit.runner.Result;
import org.junit.runner.notification.Failure;

public class MainClass {

    public static void main(String[] args) {
        Result result = JUnitCore.runClasses(Assert.class); // Assertクラスのテストを実行、変数resultに代入
        for (Failure failure : result.getFailures()) { // 失敗したテストの結果を全てfailureに代入
            System.out.println(failure.toString()); // 失敗したテストの結果を文字列にして出力
        }
        System.out.println(result.wasSuccessful()); // 全てのテストが真の場合はtrueを返す
    }

}

✅テストを実行し画像と同じ結果がコンソールに出力される

image.png

テストを修正する

  • コンソールにfalseと表示されているためどこかでテストが落ちている
  • 調べたところassertFalseでテストが落ちているため修正する必要がある
Assert.java
assertFalse(val > val1);

✅テストを実行して画像と同じ結果がコンソールの出力されている

image.png

参考にした記事(いつもありがとうございます)

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