AssertJ的“软”断言在fail()调用时断言为硬断言。
创始人
2024-09-20 16:00:30
0

在AssertJ中,软断言通过使用softAssertions对象来创建。当使用softAssertions.assertAll()方法时,如果有任何断言失败,它将记录失败的断言,但不会中止测试。

要将软断言转换为硬断言,可以使用assertThatCode方法包装需要进行断言的代码块,并在其后调用doesNotThrowAnyException()方法。这样,如果代码块中的断言失败,将抛出一个AssertionError,使其成为一个硬断言。

以下是一个示例代码:

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

public class SoftAssertExample {
    
    public void someMethod() {
        int x = 5;
        int y = 10;
        
        SoftAssertions softAssertions = new SoftAssertions();
        
        softAssertions.assertThat(x).isEqualTo(5);
        softAssertions.assertThat(y).isEqualTo(20); // This assertion will fail
        
        softAssertions.assertAll(); // Soft assertion
        
        Assertions.assertThatCode(() -> {
            Assertions.assertThat(x).isEqualTo(5);
            Assertions.assertThat(y).isEqualTo(20); // This assertion will fail
        }).doesNotThrowAnyException(); // Hard assertion
    }
    
    public static void main(String[] args) {
        SoftAssertExample example = new SoftAssertExample();
        example.someMethod();
    }
}

在上面的示例中,软断言将记录失败的断言,但不会抛出异常。然而,通过使用assertThatCode方法和doesNotThrowAnyException方法,我们可以将其转换为硬断言,如果有任何断言失败,将抛出AssertionError异常。

相关内容

热门资讯

透视软件!wpk脚本辅助器,w... 透视软件!wpk脚本辅助器,wepoker钻石怎么看底牌,科技教程(确实存在有挂)准备好在wpk脚本...
透视苹果版!红龙poker脚本... 透视苹果版!红龙poker脚本,wepoker作弊辅助,重大来袭(有挂教程)1、每个玩家都可以进行使...
透视黑科技!wepoker透视... 透视黑科技!wepoker透视脚本网页,哈糖大菠萝有挂吗5个常用方法,AI教程(素来是有挂) 科技详...
辅助透视!wepoker底牌透... 辅助透视!wepoker底牌透视脚本怎样安装,德普之星透视辅助,技术分享(有挂教程)是一款可以让一直...
透视透视!aapoker免费透... 透视透视!aapoker免费透视脚本,fishpoker透视,2025新版技巧(原本是真的有挂),支...
透视游戏!aapoker辅助器... 透视游戏!aapoker辅助器怎么用,aapoker免费透视脚本,科普攻略(有挂黑科技)1、完成aa...
透视了解!aapoker透视怎... 透视了解!aapoker透视怎么用,wepoker游戏下载,教你教程(确实有挂)1、许多玩家不知道a...
透视好牌!德普之星透视辅助软件... 透视好牌!德普之星透视辅助软件是真的吗,红龙poker透视,推荐十款(有挂方法);德普之星透视辅助软...
透视美元局!德普之星app安卓... 透视美元局!德普之星app安卓版破解版,云扑克有透视吗,实用技巧(总是真的是有挂)这是由厦门游乐互动...
透视科技!aapoker辅助器... 透视科技!aapoker辅助器怎么用,hhpoker辅助下载,重要通知(有挂辅助挂)1.aapoke...