当前位置: 首页 > 知识库问答 >
问题:

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

厉文栋
2023-03-14
    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)

我这里少了点什么。我做错什么了吗?

共有1个答案

郑嘉悦
2023-03-14

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

另外:如果在调用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;
}
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)
@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.
 类似资料:
  • null 即使抛出异常,也可以观察到(并测试)一些副作用。我的示例测试代码如下: 从抛出的异常中创建另一个软断言的最佳方法是什么?使用原始TestNG API,我可以简单地编写 但是AssertJ似乎没有提供任何类似的东西。到目前为止,我的最佳选择是向catch块添加如下所示的smth: 如何使正在测试的代码引发的异常显示为结果的原因(或禁止的异常)?目前,我做以下工作: --但更优雅的版本是非常

  • 我想测试一个特定的方法是否可以毫无例外地处理一组字符串。因此,我想使用AssertJ的软断言,比如: 不幸的是,我必须坚持使用AssertJ 1。x分别是Java 6,所以我不能利用这一点: 有没有办法用AssertJ(或JUnit)做到这一点?

  • 问题内容: 编辑:切换到一个更好的示例,并阐明了为什么这是一个真正的问题。 我想用Python编写在断言失败时继续执行的单元测试,这样我就可以在一个测试中看到多个失败。例如: 在这里,测试的目的是确保Car’s正确设置其字段。我可以将其分解为四个方法(这通常是个好主意),但是在这种情况下,我认为将其保留为测试单个概念的单个方法(“对象已正确初始化”)更容易理解。 如果我们认为最好不要破坏该方法,那

  • 我在单元测试中使用groovy脚本。我有以下代码片段,我在单个测试脚本中使用多个断言。 第一个断言失败并停止执行。但我想继续进一步的代码片段。 与selenium中的软断言类似,我应该如何收集groovy中的所有失败异常。

  • 问题内容: 我抛出了异常而不是显示失败,这是我做错了,还是我应该在线程内没有断言? 堆栈跟踪 问题答案: JUnit框架仅捕获运行测试的主线程中的断言错误。它不知道新的派生线程中的异常。为了正确执行此操作,您应该将线程的终止状态传达给主线程。您应该正确同步线程,并使用某种共享变量来指示嵌套线程的结果。 编辑: 这是可以提供帮助的通用解决方案: 您应该在构造函数中将其传递给runnable,然后只需

  • 问题内容: 有谁知道是否存在可以测试被测代码中是否抛出异常的或类似东西? 问题答案: