JUnit 5符合AssertJ

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

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

JUnit 5 – assertAll

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

List<String> 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: <false>Contains John Doe ==> expected: <true> but was: <false>

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

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

List<String> 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相同,但略有不同,即不支持从属断言。

List<String> 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<Owner> 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<Owner> 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

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/348702.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

开源GraphView的使用--数据统计

最近做室内定位需要绘出加速度传感器输出的三个方向的加速度曲线&#xff0c;找到了开源https://github.com/jjoe64/GraphView-Demos&#xff0c;省去了要重新学MatLab **。 在http://www.android-graphview.org/download--getting-started.html下载.jar包。 1、GraphView的使…

json绑定到实体_绑定到JSON和XML –处理集合

json绑定到实体EclipseLink JAXB&#xff08;MOXy&#xff09;的优势之一是能够使用单个元数据集将对象模型映射到JSON和XML。 一个弱点是您需要在JSON键或XML元素上折中集合属性。 我很高兴地说这个问题已经在EclipseLink 2.5&#xff08;和EclipseLink 2.4.2&#xff09;中解…

Java JDK 10会有什么期望

由于我们刚刚习惯于9月发布的Java 9&#xff0c;因此距离下一代Java的发布只有几个月的时间了。 就在本月&#xff0c;计划中的Java Development Kit 10升级已进入开发的主要减速阶段。 在第一个初始阶段&#xff0c;可以修复一个到三个错误。 JDK 10是Java Standard Edition …

jpa配置映射包_JPA – Hibernate –包级别的类型映射

jpa配置映射包当我们最终成熟到可以在JPA中使用某些自定义类型映射时&#xff0c;我们通常会停留在某些提供程序特定的解决方案上&#xff0c;因为JPA本身并未定义任何用于执行此操作的机制。 让我为您展示一个JPA提供程序Hibernate的自定义类型映射定义的示例。 假设我们在项目…

弹簧和线程:异步

以前&#xff0c;我们开始使用spring和TaskExecutor &#xff0c;因此我们对如何在spring应用程序中使用线程更加熟悉。 但是&#xff0c;使用任务执行程序可能比较麻烦&#xff0c;尤其是当我们需要执行简单的操作时。 Spring的异步方法可以解决。 您不必为可运行对象和Tas…

C++一天一个程序(一)

例1: helloworld! #include int main() { std::cout << “Hello, world!n”; } 或者 #include using namespaces std; int main() { cout << “Hello, world!n”; } 换行还可以endl

我的对象命名

这是最常见的辩论之一。 大多数人对此主题有自己的见解&#xff0c;却没人能真正说出哪个是正确的。 我当然不能&#xff0c;但是尽管如此&#xff0c;我还是决定与大家分享我的想法&#xff0c;投入两美分&#xff0c;也许对某人会有帮助。 当我创建一个新类时&#xff0c;我…

C++一天一个程序(二)

#include #define NUMBER 4 int main() { std::cout << NUMBER << std::endl; } 或者 #include using namespaces std; int main() { cout << 4<< endl; } 注意: 第一段中NUMBER已经被定义&#xff0c;不可以在程序中再次赋值。建议不要用#define定义…

c++编写web服务_让我们编写一个文档样式的Web服务

c编写web服务您可能知道&#xff0c;我们可以使用四种主要的Web服务样式。 它们如下&#xff1a; 文件/文学 包装的文件/文学 RPC /编码 RPC /文字 当然&#xff0c;现在不建议使用RPC /编码样式。 如果您有兴趣&#xff0c;可以在此处找到这篇非常全面的文章&#xff0c;…

C++一天一个程序(三)

#include <stdio.h> class Trace { public:     Trace()  {noisy 0; }      void print(char* s)  { if (noisy)  printf("%s",s); }   void on()  { noisy 1; }      void off()   { noisy 0; }  private: int noisy;   };  C改…

Java 9:ServiceLoader

java.util.ServiceLoader类在运行时加载服务提供者/实现。 在编译时&#xff0c;ServiceLoader只需要知道Service接口。 借助Java9模块化&#xff0c;我们可以在运行时动态添加服务实现模块&#xff0c;而Application可以拥有新的实现&#xff0c;而不会影响任何事情&#xff0…

C++一天一个程序(四)

#include using namespace std; struct complex{  double real, imag;  complex(double 0.0, double 0.0); } complex&#xff1a;complex(double r, double i) {  real r; imag i; } inline ostream& operator<<(ostream &os, const complex &c) {…

C++一天一个程序(五)

(1)确定所求长方形的长和宽。 (2)确定计算长方形的周长和面积的公式并计算。 (3)输出计算结果。 (1)以面向过程程序设计思想编码。 #include using namespace std; void main(){ int perimeter,area; int length20,width10; perimeter2*(lengthwidth); arealength* width; cou…

netbeans 定制代码_将NetBeans代码模板弯曲到我的意愿

netbeans 定制代码任何阅读过我关于NetBeans的文章的人都知道&#xff0c;我真的很喜欢NetBeans的许多功能。 但是&#xff0c;最近&#xff0c;我发现自己对NetBeans特定功能的特定问题越来越恼火。 最终&#xff0c;它使我烦恼不已&#xff0c;促使我开始研究如何根据自己的喜…

一天一个C++程序(六)

数据类型转换应用示例。 #include using namespace std; int main() { int a,c,d,b322; float x,z,y4.56; char ch1‘d’,ch2; ay; xb; cch1; ch2b; zyb; dbch1; cout<<“a”<<a<<"\tx"<<x<<endl; cout<<“c”<<c<<…

早期更多失败– Java 8

快速失败或早期失败是一种软件工程概念&#xff0c;旨在通过在不应该发生的事情发生时立即停止执行来防止复杂问题的发生。 在之前的博客文章和演示中&#xff0c;我将详细介绍这种方法的优点&#xff0c;在此博客文章中&#xff0c;我将详细介绍Java 8中该思想的另一种用法。 …

C++一天一个程序(七)

#include using namespace std; int main() { cout<<“字符型 (char)所占字节数:”<<sizeof(char)<<endl; cout<<“无符号字符型(unsigned char)所占字节数:”<<sizeof( unsigned char)<<endl; cout<<“短整型( short int)所占字节数…

C++一天一个程序(八)

#include <iostream.h> void main() { int i,j,k; for(i1;i<6;i) { for(j1:j<6-i;j) cout<<" “; for(kl;k<: 2i- 1;k) cout<<"%"; cout<<endl; } for(i5;i>1;–) { for(j 1:j<6-i:j) cout<<" "; for(k…

url中传递对象参数_在URL参数中传递复杂对象

url中传递对象参数假设您想传递原始数据类型&#xff0c;例如复杂的Java对象 java.util.Data&#xff0c;java.lang.List&#xff0c;泛型类&#xff0c;数组以及通过URL参数所需的所有内容&#xff0c;以便在页面加载后在任何网页上预设默认值。 共同的任务&#xff1f; 是的…

C++两天一个程序(一)

#include  using namespace std;  main()   {    int  i 7;    int* ip &i;    int** ipp &ip;    cout << "Address " << ip << " contains " << *ip << endl;    cout << "A…