乘以 AssertJ 断言中设置的条件?

a.k*_*a.k 6 java junit unit-testing assertj

我正在尝试在 assertJ 上设置乘法条件,但在 examplesGit 中找不到。

我目前写:

    assertThat(A.getPhone())
            .isEqualTo(B.getPhone());
    assertThat(A.getServiceBundle().getId())
            .isEqualTo(B.getServiceBundle().getId());
Run Code Online (Sandbox Code Playgroud)

但想要有类似的东西:

            assertThat(A.getPhone())
            .isEqualTo(B.getPhone())
            .And
            (A.getServiceBundle().getId())
            .isEqualTo(B.getServiceBundle().getId());
Run Code Online (Sandbox Code Playgroud)

好像我使用链接这行不通,因为我需要差异数据(id 而不是电话)。有没有可能将它全部混合到一个单一的assertJ命令中?看起来似乎没有任何可能性(算法明智),但也许还有一些其他想法可以在语句上使用 && ?

谢谢

gil*_*des 9

您可以在 AssertJ 中使用软断言来组合多个断言并一次性评估这些断言。软断言允许组合多个断言,然后在一个操作中评估它们。它有点像事务性断言。您设置断言包,然后提交它。

SoftAssertions phoneBundle = new SoftAssertions();
phoneBundle.assertThat("a").as("Phone 1").isEqualTo("a");
phoneBundle.assertThat("b").as("Service bundle").endsWith("c");
phoneBundle.assertAll();
Run Code Online (Sandbox Code Playgroud)

它有点冗长,但它是“&&”-ing 断言的替代方法。错误报告实际上非常细化,因此它指向失败的部分断言。所以上面的例子将打印:

org.assertj.core.api.SoftAssertionError: 
The following assertion failed:
1) [Service bundle] 
Expecting:
 <"b">
to end with:
 <"c">
Run Code Online (Sandbox Code Playgroud)

实际上,由于详细的错误消息,这比“&&”选项更好。


her*_*ung 5

断言J的替代品SoftAssertions是JUnit的assertAll

import static org.junit.jupiter.api.Assertions.assertAll;

assertAll(
  () -> assertThat("a").as("Phone 1").isEqualTo("a"),
  () -> assertThat("b").as("Service bundle").endsWith("c")
);
Run Code Online (Sandbox Code Playgroud)