Testing 软断言导致后续测试失败时引发的异常

Testing 软断言导致后续测试失败时引发的异常,testing,assertj,Testing,Assertj,根据标题,我正试图在循环中运行一个测试用例。为了能够计算失败断言的数量,我希望如果AssertJ试图断言从方法调用返回的值,它应该轻轻地使单个迭代失败并继续。否则,它就违背了软断言的目的。下面是一个示例: public static void main(String[] args) { SoftAssertions softAssertions = new SoftAssertions(); softAssertions.assertThat(throwE

根据标题,我正试图在循环中运行一个测试用例。为了能够计算失败断言的数量,我希望如果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)
org.opentest4j.MultipleFailuresError: Check condition (2 failures)
    <no message> in java.lang.IllegalArgumentException

Expecting:
 <true>
to be equal to:
 <false>
but was not.

我在这里遗漏了一些东西。我做错了什么吗?

根据我的理解,软断言处理布尔值,而不是异常

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

只要试着调试代码,就会发现从未调用过
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;
}
这将输出多个断言的结果,并且不会在评估第一个失败的断言时停止

输出: 您将在输出中看到这一点:

   Exception in thread "main" java.lang.RuntimeException
    at eLCMUpdate.throwException(MyClass.java:101)
    at eLCMUpdate.main(MyClass.java:95)
org.opentest4j.MultipleFailuresError: Check condition (2 failures)
    <no message> in java.lang.IllegalArgumentException

Expecting:
 <true>
to be equal to:
 <false>
but was not.
org.opentest4j.MultipleFailuresError:检查条件(2次失败)
在java.lang.IllegalArgumentException中
期望:
等于:
但事实并非如此。

代码中的问题
softAssertions.assertThat(throweexception(10)).isTrue()
是指如果抛出异常,则断言根本不执行

您需要的是对传入的代码进行惰性评估,您可以使用AssertJ
assertThatCode
执行此操作,如下所示:

final SoftAssertions softAssertions = new SoftAssertions();
softAssertions.assertThatCode(() -> throwException(10)).doesNotThrowAnyException();
softAssertions.assertThatCode(() -> throwException(1)).isInstanceOf(RuntimeException.class);
softAssertions.assertAll();

这只是为了测试AssertJ如何处理异常。我的实际代码有一个方法调用,这个方法容易引发异常。我总是可以捕捉异常并处理它们,但对我来说这似乎不合逻辑,正如我所说的,这违背了软断言的目的。非常感谢您提供的信息。由于某些原因,我似乎无法获得您得到的输出:/Which assert您正在使用的函数?在我看来,它似乎来自AssertJ。请查看
assertAll
上的JUnit5指南示例。看
assertAll
可以帮助您,因为它根据您的需要处理异常。如果您的答案正确,我很乐意接受。我就是不能得到你得到的结果。你能帮我指出我做错了什么吗?@enissay,我已经创建了一个GitHub repository maven项目,其中包含此测试的代码,你可以克隆并试用:。还请注意,我没有发布测试的完整冗长输出,但我遗漏了测试的某些部分。但相关信息确实在帖子中。我看到了这个选项,但问题是,我无法预测我可能会遇到的所有异常。因为如果我不这样做的话,一个疯狂的NPE可能会让我的测试失败。不过,
assertThatCode
想法+1。