代码之家  ›  专栏  ›  技术社区  ›  enissay

在软断言使后续测试失败时引发的异常

  •  1
  • enissay  · 技术社区  · 8 年前

    根据标题,我正在尝试在循环中运行测试用例。为了能够计算失败断言的数量,我希望如果AssertJ试图断言从方法调用返回的值,它应该 轻轻地 失败一次迭代并继续。否则,它违背了软断言的目的。下面是一个示例:

        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)
    

    2 回复  |  直到 8 年前
        1
  •  2
  •   Joel Costigliola    8 年前

    代码中的问题 softAssertions.assertThat(throwException(10)).isTrue(); 如果抛出异常 assertThat 根本没有执行。

    您需要的是延迟评估您正在传递的代码 断言 ,您可以使用AssertJ执行此操作 assertThatCode 如下所示:

    final SoftAssertions softAssertions = new SoftAssertions();
    softAssertions.assertThatCode(() -> throwException(10)).doesNotThrowAnyException();
    softAssertions.assertThatCode(() -> throwException(1)).isInstanceOf(RuntimeException.class);
    softAssertions.assertAll();
    
        2
  •  1
  •   gil.fernandes    8 年前

    另外:如果在调用之前引发异常 softAssertions.assertAll() 显然,这种方法也永远不会被执行。这实际上是您报告的行为的原因。

    只要试着调试代码,您就会看到

    如果您将代码更改为:

    @Test
    void soft_assertions() {
        SoftAssertions softAssertions = new SoftAssertions();
        softAssertions.assertThat(checkCondition(10)).isTrue();
        softAssertions.assertThat(checkCondition(10)).isTrue();
        softAssertions.assertThat(checkCondition(1)).isTrue();
        softAssertions.assertThat(checkCondition(2)).isTrue();
        softAssertions.assertThat(checkCondition(20)).isTrue();
        softAssertions.assertAll();
    }
    
    private static boolean checkCondition(int stuff){
        if(stuff == 1 || stuff == 2){
            return false;
        }
        return true;
    }
    

    这将输出多个断言的结果,并且不会在评估第一个失败的断言时停止。

    输出:

    org.assertj.core.api.SoftAssertionError: 
    The following 2 assertions failed:
    1) 
    Expecting:
     <false>
    to be equal to:
     <true>
    but was not.
    at JsonStewardshipCustomerConversionTest.soft_assertions(JsonStewardshipCustomerConversionTest.java:301)
    2) 
    Expecting:
     <false>
    to be equal to:
     <true>
    but was not.
    at JsonStewardshipCustomerConversionTest.soft_assertions(JsonStewardshipCustomerConversionTest.java:302)
    

    SoftAssertion似乎不符合您的目的。

    我建议你改用JUnit 5 assertAll . 根据我的测试,它评估了 阻塞和生存异常。这里的问题是您需要JUnit 5,它可能还没有被广泛采用。

    这是一个布尔条件失败的示例,也是一个异常。这两个都在控制台中报告。

    @Test
    void soft_assertions() {
        assertAll("Check condition",
                () -> assertThat(checkCondition(9)).isTrue(),
                () -> assertThat(checkCondition(10)).isTrue(),
                () -> assertThat(checkCondition(11)).isTrue(),
                () -> assertThat(checkCondition(2)).isTrue(), // Throws exception
                () -> assertThat(checkCondition(3)).isFalse(), // fails
                () -> assertThrows(IllegalArgumentException.class, () -> {
                    checkCondition(1);
                })
        );
    }
    
    private static boolean checkCondition(int stuff) {
        if (stuff == 1 || stuff == 2) {
            throw new IllegalArgumentException();
        }
        return true;
    }
    

    org.opentest4j.MultipleFailuresError: Check condition (2 failures)
        <no message> in java.lang.IllegalArgumentException
    
    Expecting:
     <true>
    to be equal to:
     <false>
    but was not.