当前位置:网站首页>Exception assertion of assertj
Exception assertion of assertj
2022-06-12 16:26:00 【HoneyMoose】
This page mainly talks about AssertJ Exception assertion for .
summary
In this quick navigation , We are mainly here to talk about AssertJ It's abnormal (exception) Assertion .
of AssertJ Project , Please visit AssertJ / Fluent assertions for java page .
Don't use AssertJ
If not used AssertJ, We need to catch an exception first , Then make assertions in the exception .
For example, the following pseudo code , We caught an exception , And then judge .
try {
// ...
} catch (Exception e) {
// assertions
}however , If the program does not throw an exception during execution , In the above use case , The test will pass .
In order for assertions to be executed , Why do we need to manually trigger an exception ?
Use AssertJ
stay Java 8 Later versions , We can do that by using AssertJ and lambda expression , It's very easy to assert exceptions .
Use assertThatThrownBy() Method
Let's see what the following code will throw IndexOutOfBoundsException abnormal :
This is because we define a List Is the length of the 2 , But we have a line of code that will access List Of the 3 Elements , The above code must throw an exception .
assertThatThrownBy(() -> {
ArrayList<String> myStringList = new ArrayList<String>(Arrays.asList("Strine one", "String two"));
myStringList.get(2);
}).isInstanceOf(IndexOutOfBoundsException.class)
.hasMessageStartingWith("Index 2");Note that the code snippet above may throw a lambda Expression exception .
Of course , We can also use AssertJ To provide a chained assertion , That's why we don't use Junit Reason for self assertion .
.hasMessageStartingWith("Index 2")
.hasMessageContaining("2")
.hasMessageEndingWith("length 2")
.hasMessageContaining("Index 2")
.hasNoCause();Use assertThatExceptionOfType Method
This method is similar to the method used above , Because we know that the execution of this program will throw an exception , So we specify exceptions at the very beginning of the program :
assertThatExceptionOfType(ArithmeticException.class).isThrownBy(() -> {
int numerator = 10;
int denominator = 0;
int quotient = numerator / denominator;
})
.withMessageContaining("/ by zero");The above method , Because we used 0 Divisor , So an exception must be thrown .
We asserted this exception at the very beginning of the program .
Use assertThatIOException And other common types
AssertJ in the light of Some common exceptions are wrapped , You can use these wrapped exceptions directly :
assertThatIOException().isThrownBy(() -> {
// ...
});And other similar exceptions :
- assertThatIllegalArgumentException()
- assertThatIllegalStateException()
- assertThatIOException()
- assertThatNullPointerException()
Detach exceptions from assertions
Optional , We can separate exceptions from assertions .
The way to separate is to add one when and then Logical segment :
// when
Throwable thrown = catchThrowable(() -> {
int numerator = 10;
int denominator = 0;
int quotient = numerator / denominator;
});
// then
assertThat(thrown).isInstanceOf(ArithmeticException.class)
.hasMessageContaining("/ by zero");
}The above code first throws an exception , Then, assert and judge the thrown exception .
Conclusion
In this essay , We are right. AssertJ How to make exception assertions is briefly introduced , It also discusses AssertJ How to make exception assertions .
边栏推荐
- 收藏 | 22个短视频学习Adobe Illustrator论文图形编辑和排版
- acwing795 前缀和(一维)
- 连续八年包装饮用水市占率第一,这个品牌DTC是如何持续增长的?
- Global and Chinese market for commercial ceiling fans 2022-2028: Research Report on technology, participants, trends, market size and share
- Thread pool execution process
- 1.delete
- JS monitors whether the user opens the screen focus
- Super detailed dry goods! Docker+pxc+haproxy build a MySQL Cluster with high availability and strong consistency
- Servlet API
- generate pivot data 0
猜你喜欢

Thinking about the probability of drawing cards in the duel link of game king

超详细干货!Docker+PXC+Haproxy搭建高可用强一致性的MySQL集群

Project training of Shandong University rendering engine system (III)

读取mhd、raw图像并切片、归一化、保存

Multimix:从医学图像中进行的少量监督,可解释的多任务学习

批量--03---CmdUtil

WebRTC 的音频网络对抗概述

MYSQL---服务器配置相关问题

Overview of webrtc's audio network Countermeasures

acwing 801. Number of 1 in binary (bit operation)
随机推荐
The market share of packaged drinking water has been the first for eight consecutive years. How does this brand DTC continue to grow?
MySQL面试整理
33-【go】Golang sync.WaitGroup的用法—保证go协程执行完毕,主协程才退出
acwing 802. 区间和 (离散化)
Postgresql源码(53)plpgsql语法解析关键流程、函数分析
WebRTC 的音频网络对抗概述
1.delete
Glove word embedding (IMDb film review emotion prediction project practice)
Project training of Shandong University rendering engine system (V)
GloVe词嵌入(IMDB电影评论情感预测项目实战)
收藏 | 22个短视频学习Adobe Illustrator论文图形编辑和排版
acwing 790. 数的三次方根(浮点数二分)
acwing 801. Number of 1 in binary (bit operation)
Statistical machine learning code set
std::set compare
RTOS RT thread bare metal system and multi thread system
The C programming language (2nd Edition) notes / 7 input and output / 7.8 other functions
面试:为什么整数包装类尽量用equals()来比较大小
Global and Chinese markets for air sampling calibration pumps 2022-2028: Research Report on technology, participants, trends, market size and share
calibration of sth