Assertj的SoftAssertions抛出异常
创始人
2024-09-20 16:00:29
0

SoftAssertions是Assertj的一种断言类型,它可以在一个测试中收集多个失败的断言而不立即抛出AssertionError。然而,在使用SoftAssertions进行断言时,如果存在断言失败导致最终测试失败的情况,Assertj会抛出AssertionError作为失败的测试的结果。

但是,有时候我们希望在出现失败的情况时,不抛出异常,而是继续执行测试并记录失败。为了实现这个功能,可以在SoftAssertions对象的构造函数中传入一个参数,详细说明了需要抑制的异常类型。

以下代码示例演示如何使用SoftAssertions并抑制AssertionError异常:

import org.assertj.core.api.Assertions;
import org.assertj.core.api.SoftAssertions;

public class SoftAssertionsDemo {

    public void testSoftAssertions() {
        SoftAssertions softAssertions = new SoftAssertions().dontFailOnAssertionError();

        String actualString = "hello world";
        String expectedString = "Hello world";

        softAssertions.assertThat(actualString).isEqualToIgnoringCase(expectedString);
        softAssertions.assertThat(actualString).startsWith("hello");
        softAssertions.assertThat(actualString).endsWith("ld");

        softAssertions.assertAll(); // 捕获SoftAssertionError异常,继续执行并记录失败

        // 在SoftAssertions后添加原始的断言方法也同样适用
        Assertions.assertThat(actualString).isNotEqualTo(expectedString);
    }
}

在这个示例中,我们使用了SoftAssertions的dontFailOnAssertionError()方法在出现失败情况时不立即抛出异常。相反地,使用assertAll()方法来捕获SoftAssertionError异常以继续执行并记录失败。此外,我们还添加了Assertj的原始断言方法来演示如何在SoftAssertions后添加原始的断言方法。

通过这种方式,我们可以在Assertj中使用SoftAssertions并捕获异常来实现记录断言失败但继续测试的功能。

相关内容

热门资讯

透视好友!德普之星辅助器app... 透视好友!德普之星辅助器app,aapoker透视脚本下载,玩家分析(有挂总结);人气非常高,ai更...
透视私人局!wpk辅助软件多少... 透视私人局!wpk辅助软件多少钱一个,wepoker正确养号方法,德州论坛(总是是有挂);是一款可以...
透视安卓版!wpk透视工作室,... 透视安卓版!wpk透视工作室,aapoker俱乐部靠谱吗,新手必备(有挂方法)1、超多福利:超高返利...
透视挂!hhpoker透视脚本... 透视挂!hhpoker透视脚本视频,德州机器人代打脚本,规律教程(从来真的是有挂)1、这是跨平台的h...
透视挂透视!wepoker底牌... 透视挂透视!wepoker底牌透视脚本app,hhpoker辅助下载,专业讨论(有挂了解)1、玩家可...
透视透视!德普之星app安卓版... 透视透视!德普之星app安卓版破解版,werplan外卦神器,切实教程(最初存在有挂);1分钟了解详...
透视脚本!wepoker作弊辅... 1、透视脚本!wepoker作弊辅助,wepoker透视脚本下载,揭秘攻略(有挂教程);代表性(透视...
透视神器!德普之星辅助功能如何... 透视神器!德普之星辅助功能如何打开,德州辅助工具到底怎么样,曝光教程(原来真的是有挂)1、完成德普之...
透视好友!wpk脚本下载,hh... 透视好友!wpk脚本下载,hhpoker透视脚本安卓,实操分享(有挂总结)1、起透看视 hhpoke...
透视透视!德普之星有没有挂,约... 透视透视!德普之星有没有挂,约局吧辅助器,存在挂教程(竟然存在有挂);揭秘教程安装方法样式多选,攻略...