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

Multiply conditions set in AssertJ assertions?

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

我目前在写:

    assertThat(A.getPhone())
            .isEqualTo(B.getPhone());
    assertThat(A.getServiceBundle().getId())
            .isEqualTo(B.getServiceBundle().getId());

但是想要这样的东西:

            assertThat(A.getPhone())
            .isEqualTo(B.getPhone())
            .And
            (A.getServiceBundle().getId())
            .isEqualTo(B.getServiceBundle().getId());

就像我使用链接一样,这不起作用,因为我需要差异数据(id 而不是 phone)。有没有可能将它全部混合到一个 one-assertJ 命令中?看起来没有任何可能性(算法方面),但也许有其他关于 && on statements 的想法?

谢谢

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

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

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

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

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

assertJ 的 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")
);