当前位置:网站首页>Yyds dry goods inventory junit5 learning II: assumptions class
Yyds dry goods inventory junit5 learning II: assumptions class
2022-06-24 22:22:00 【Programmer Xinchen】
Welcome to visit mine GitHub
Here we classify and summarize all the original works of Xinchen ( Including supporting source code ): https://github.com/zq2599/blog_demos
An overview of this article
This article is about 《JUnit5 Study 》 The second part of the series , Learn an important knowledge point :Assumptions class , Only by understanding them , Only then can continue the later study , The whole chapter is as follows :
- Assertions and Assumptions brief introduction
- Write a piece of code to compare the effect
- Assumptions code
- View the execution results
Source download
- If you don't want to code , Can be in GitHub Download all the source code , The address and link information is shown in the following table :
| name | link | remarks |
|---|---|---|
| Project home page | https://github.com/zq2599/blog_demos | The project is in progress. GitHub Home page on |
| git Warehouse address (https) | https://github.com/zq2599/blog_demos.git | The warehouse address of the source code of the project ,https agreement |
| git Warehouse address (ssh) | [email protected]:zq2599/blog_demos.git | The warehouse address of the source code of the project ,ssh agreement |
This git Multiple folders in project , The application of this chapter in junitpractice Under the folder , As shown in the red box below :

junitpractice It's a father son structure project , The code of this article is in assertassume In the subproject , Here's the picture :

Assertions and Assumptions brief introduction
Assumptions and Assertions Easy to confuse , So here we learn by comparing them :
- Assertions The assertion class , It provides a lot of static methods , for example assertTrue, If assertTrue The input parameter of is false, Will throw AssertionFailedError abnormal ,Junit The method that throws this exception is judged as failure ;
- Assumptions The hypothetical class , It provides a lot of static methods , for example assumeTrue, If assumeTrue The input parameter of is false, Will throw TestAbortedException abnormal ,Junit The method that throws this exception is judged as skipping ;
- To put it simply ,Assertions To throw an exception means that the test fails ,Assumptions Means that the test is skipped ( Why is it called " skip "? because mvn test The execution result of is marked as Skipped);
Write a piece of code to compare the effect
- Code is the best way to verify it , As shown below , There are four ways ,assertSuccess Do not throw AssertionFailedError abnormal ,assertFail Throw out AssertionFailedError abnormal ,assumpSuccess Do not throw TestAbortedException abnormal ,assumpFail Throw out TestAbortedException abnormal
- Click the red box in the figure below to execute unit test :

- The results are as follows :

- in addition , stay target Catalog , You can see surefire Unit test reports generated by plug-ins TEST-com.bolingcavalry.assertassume.service.impl.AssertAssumpTest.xml, As shown in the figure below ,testcase In the node skipped node :

- The above comparative verification shows again that Assertions and Assumptions The difference between : Are used to compare the expected value with the actual value , When the expected value is inconsistent with the actual value ,Assertions The result of the test is execution failure ,Assumptions The test result is to skip ( Or ignore );
Assumptions actual combat
It's clear Assertions and Assumptions The difference between , Next, strike while the iron is hot , Study Assumptions Several important static methods in class :assumeTrue、assumingThat
- The simplest usage is as follows , It can be seen that only assumeTrue Don't throw exceptions , hinder log.info Will execute :
- assumeTrue Acceptable Supplier Type as the second input , If assumeTrue Failure will take the content of the second parameter as a failure prompt :
The effect is as follows :
3. There's another. assumingThat Method , Acceptable Executable Type as the second input , If the first input is true Will execute Executable Of execute Method , Be careful assumingThat Characteristics of the method : Don't throw exceptions , So the method it's in won't be skipped , This is the assumeTrue Compared with the biggest difference (assumeTrue Once entered, it is false Will throw an exception , Its method is marked as skip ):
- Next, let's execute the above code , Look at the effect ;
perform Assumptions Code
- Do the preparatory work first , This actual battle springboot Engineering, assertassume, We are working on resources Add two configuration files to the directory :application.properties and application-test.properties, The location is as follows :

- application-test.properties The contents are as follows :
- application.properties The contents are as follows :
- The complete unit test classes are as follows , Through annotation ActiveProfiles, Specified the use of application-test.properties Configuration of , therefore envType The value of is CI:
The results are shown in the following figure , so assume adopt , All the information has been printed out :

Next, put the... In the code ActiveProfiles Note out the line of notes , The red box is shown below :

The results are as follows , so tryAssumingThat Methods are marked as successful , But from the log we can see assumingThat The second entry of executable Not implemented :

- thus ,Assumptions Class common method experience complete , The following sections will continue to learn about other common classes ;
Welcome to your attention 51CTO Blog : Xinchen, programmer
On the way to study , You are not alone , Xinchen's original works are accompanied all the way …
边栏推荐
- 降低pip到指定版本(通過PyCharm昇級pip,在降低到原來版本)
- Collective search + drawing creation
- You are using pip version 21.1.2; however, version 22.1.2 is available
- Process communication mode
- Interviewer: you said you are proficient in redis. Have you seen the persistent configuration?
- In the first year of L2, arbitrum nitro was upgraded to bring more compatible and efficient development experience
- Detailed explanation of agency mode
- Maximum flow problem
- 波卡生态发展不设限的奥义——多维解读平行链
- 虚拟人的产业发展现状
猜你喜欢

Machine learning: linear regression
![leetcode:515. Find the maximum value in each tree row [brainless BFS]](/img/87/1926d783fb6f8d8439213d86b5da40.png)
leetcode:515. Find the maximum value in each tree row [brainless BFS]

What aspects should we start with in the feasibility analysis of dry goods?

KT6368A蓝牙双模透传芯片软件版本选型说明

first-order-model实现照片动起来(附工具代码) | 机器学习

NIO 零拷贝

嵌入式开发:技巧和窍门——干净地从引导加载程序跳转到应用程序代码

Common voting governance in Dao

L2 元年,Arbitrum Nitro 升级带来更兼容高效的开发体验

权限想要细化到按钮,怎么做?
随机推荐
I really want to send a bunch of flowers
Docker 安装 Redis-5.0.12,详细步骤
Description of software version selection of kt6368a Bluetooth dual-mode transparent chip
CV2 package guide times could not find a version that satisfies the requirement CV2 (from versions: none)
Raspberry pie preliminary use
系统测试主要步骤
NIO、BIO、AIO
I really can't do it. After 00, I collapsed and wanted to leave
img2pdf
After Firefox drag and drop, Baidu search will always be opened by default. If it is a picture, the picture will be opened.
Flutter: Unsupported value: false/true
Flutter 如何使用在线转码工具将 JSON 转为 Model
Rotate the square array of two-dimensional array clockwise by 90 °
Valueerror: cannot take a larger sample than population when 'replace=false‘
Uncover the secret of station B. is it true that programmers wear women's clothes and knock code more efficiently?
短视频商城系统,scroll-view如何自适应页面剩余高度
leetcode:515. Find the maximum value in each tree row [brainless BFS]
Zero code can apply data visualization to enterprise management
leetcode:55. 跳跃游戏【经典贪心】
Why can some programmers get good offers with average ability?