junit5和junit4
JUnit 5在断言库中带来了很多改进,这主要归功于Java 8和Lambda Expression的支持以及新断言(如assertAll
, assertTimeout
或assertThrows
。 尽管我非常喜欢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