建筑工程素材资源网站做cpa一定要有网站
web/
2025/9/30 6:53:42/
文章来源:
建筑工程素材资源网站,做cpa一定要有网站,wordpress只显示代码,上海网站制作是什么junit5和junit4JUnit 5在断言库中带来了很多改进#xff0c;这主要归功于Java 8和Lambda Expression的支持以及新断言#xff08;如assertAll #xff0c; assertTimeout或assertThrows 。 尽管我非常喜欢JUnit 5#xff0c;但我相信AssertJ在生产级单元测试中仍然是必不可… 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断言所有提供的可执行文件均不会引发异常 ListString 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: true but was: falseContains John Doe expected: true but was: false assertAll执行所有传递的可执行文件并确保所有传递不引发异常。 换句话说 assertAll允许分组的断言。 另外 assertAll可用于创建从属断言 ListString 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略有不同不支持从属断言。 ListString 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
OptionalOwner 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
OptionalOwner 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.htmljunit5和junit4
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/web/84317.shtml
如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!