1

タイトルのとおり、ループでテスト ケースを実行しようとしています。失敗したアサーションの数を計算できるようにするために、AssertJ がメソッド呼び出しから返された値をアサートしようとしている場合、1 回の反復をソフトに失敗させて続行することを期待しています。そうしないと、ソフト アサーションの目的に反します。これを説明するスニペットを次に示します。

    public static void main(String[] args) {
        SoftAssertions softAssertions = new SoftAssertions();
        softAssertions.assertThat(throwException(10)).isTrue();
        softAssertions.assertThat(throwException(10)).isTrue();
        softAssertions.assertThat(throwException(1)).isTrue();
        softAssertions.assertAll();
    }

    private static boolean throwException(int stuff){
        if(stuff == 1){
           throw new RuntimeException();
       }
       return true;
    }

出力:

   Exception in thread "main" java.lang.RuntimeException
    at eLCMUpdate.throwException(MyClass.java:101)
    at eLCMUpdate.main(MyClass.java:95)

ここで何かが欠けています。私は何か間違ったことをしていますか?

4

2 に答える 2

2

コードの問題はsoftAssertions.assertThat(throwException(10)).isTrue();、例外がスローされた場合、assertThatまったく実行されないことです。

必要なのは、渡すコードを遅延評価することです。以下のようassertThatに AssertJ を使用してこれを行うことができます。assertThatCode

final SoftAssertions softAssertions = new SoftAssertions();
softAssertions.assertThatCode(() -> throwException(10)).doesNotThrowAnyException();
softAssertions.assertThatCode(() -> throwException(1)).isInstanceOf(RuntimeException.class);
softAssertions.assertAll();
于 2017-10-12T00:26:15.033 に答える