junit5和junit4_JUnit 5符合AssertJ

junit5和junit4

JUnit 5在断言库中带来了很多改进,这主要归功于Java 8和Lambda Expression的支持以及新断言(如assertAllassertTimeoutassertThrows 。 尽管我非常喜欢JUnit 5,但我相信AssertJ在生产级单元测试中仍然是必不可少的,并且我将继续使用它。

但是我认为在单个单元测试中可能会混合使用JUnit 5和AssertJ:其中之一是将JUnit assertAll与AssertJ assertThat混合。

JUnit 5 – assertAll

Assertions.assertAll断言所有提供的可执行文件均不会引发异常:

List owners = Arrays.asList("Betty Davis", "Eduardo Rodriquez");

// assert
assertAll(
    () -> assertTrue(owners.contains("Betty Doe"), "Contains Betty Doe"),
    () -> assertTrue(owners.contains("John Doe"), "Contains John Doe"),
    () -> assertTrue(owners.contains("Eduardo Rodriquez"), "Eduardo Rodriquez")
);

上面将报告2个错误:

org.opentest4j.MultipleFailuresError: Multiple Failures (2 failures)
    Contains Betty Doe ==> expected:  but was: 
    Contains John Doe ==> expected:  but was: 

assertAll执行所有传递的可执行文件,并确保所有传递(不引发异常)。 换句话说, assertAll允许分组的断言。

另外, assertAll可用于创建从属断言:

List owners = Arrays.asList("Betty Davis", "Eduardo Rodriquez");

// assert
assertAll(
    () -> {
        assertTrue(owners.contains("Betty Doe"), "Contains Betty Doe");

        assertAll(
            () -> assertNotNull(owners),
            () -> assertTrue(owners.size() > 1)
        );
    }
);

在上面的例子中,当第一assertTrue失败后续assertAll将被跳过。

AssertJ –软断言

注意:我在本文中写了有关SoftAssertions的更多信息: AssertJ软断言–我们需要它们吗?

AssertJ提供的SoftAssertions基本上与JUnit 5 assertAll相同, assertAll略有不同,不支持从属断言。

List owners = Arrays.asList("Betty Davis", "Eduardo Rodriquez");

assertSoftly(
    softAssertions -> {
        softAssertions.assertThat(owners).contains("Betty Doe");
        softAssertions.assertThat(owners).contains("John Doe");
        softAssertions.assertThat(owners).contains("Eduardo Rodriquez");
    }
);

报告的错误:

1) 
Expecting:
 <["Betty Davis", "Eduardo Rodriquez"]>
to contain:
 <["Betty Doe"]>
but could not find:
 <["Betty Doe"]>

at AssertJAssertionsTest.lambda$assertsSoftly$0(AssertJAssertionsTest.java:26)
2) 
Expecting:
 <["Betty Davis", "Eduardo Rodriquez"]>
to contain:
 <["John Doe"]>
but could not find:
 <["John Doe"]>

将JUnit assertAll与AssertJ assertThat混合

混合JUnit的5 assertAll与AssertJ assertThat断言似乎是一个不错的选择:

// arrange
String givenName = "Jean";
String expectedCity = "Monona";
String expectedAddress = "105 N. Lake St.";

// act
Optional result = testObj.findByName(givenName);

// assert
assertThat(result).isPresent();

assertAll(
    () -> assertThat(result.get().getFirstName()).isEqualTo(givenName),
    () -> assertThat(result.get().getCity()).isEqualTo(expectedCity),
    () -> assertThat(result.get().getAddress()).isEqualTo(expectedAddress)
);

另一方面, assertAll可以用作assertThat的参数:

// arrange
String givenName = "Jean";
String expectedCity = "Monona";
String expectedAddress = "105 N. Lake St.";

// act
Optional result = testObj.findByName(givenName);

// assert
assertThat(result).hasValueSatisfying(owner -> assertAll(
    () -> assertThat(owner.getFirstName()).isEqualTo(givenName),
    () -> assertThat(owner.getCity()).isEqualTo(expectedCity),
    () -> assertThat(owner.getAddress()).isEqualTo(expectedAddress)
));

摘要

尽管JUnit 5是一个很棒的框架,它为断言提供了很多东西,但我认为无论如何还是需要像AssertJ这样的第三方断言库来增加断言的趣味性。 我已经使用AssertJ几年了,我认为我不会放弃它。 但是我肯定在测试中看到了新的JUnit 5 assertAll的空间。 特别是在集成测试中。

可以在以下GitHub存储库中找到此博客文章(以及更多内容)中的所有示例: https : //github.com/kolorobot/junit5-samples *

*非常感谢Maciej Koziara为该资源库做出了贡献。

翻译自: https://www.javacodegeeks.com/2017/11/junit-5-meets-assertj.html

junit5和junit4

你可能感兴趣的:(java,人工智能,单元测试,python,linux)