当前位置:网站首页>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 …
边栏推荐
- 60 个神级 VS Code 插件!!
- Excel layout
- Detailed explanation of agency mode
- Machine learning: linear regression
- TCP RTT measurement tips
- CV2 package guide times could not find a version that satisfies the requirement CV2 (from versions: none)
- Object. Defineproperty and reflect Fault tolerance of defineproperty
- leetcode:55. 跳跃游戏【经典贪心】
- 性能测试工具wrk安装使用详解
- leetcode:515. 在每个树行中找最大值【无脑bfs】
猜你喜欢

Ansible basic configuration

代理模式详解
![[200 opencv routines] 209 Color image segmentation in HSV color space](/img/fa/9a40015cbcf9c78808f147e510be4c.jpg)
[200 opencv routines] 209 Color image segmentation in HSV color space

These map operations in guava have reduced my code by 50%

How to automatically remove all . orig files in Mercurial working tree?

EasyBypass

First order model realizes photo moving (with tool code) | machine learning

网上立案流程

降低pip到指定版本(通過PyCharm昇級pip,在降低到原來版本)

You are using pip version 21.1.2; however, version 22.1.2 is available
随机推荐
Double linked list implementation
St Table + two points
socket done
leetcode:55. 跳跃游戏【经典贪心】
Binary search tree template
NIO、BIO、AIO
The logic of "Ali health" has long changed
The profound meaning of unlimited ecological development in Poka -- Multidimensional Interpretation of parallel chain
关于自动控制原理资料更新
First order model realizes photo moving (with tool code) | machine learning
Flutter: Unsupported value: false/true
Want to be a test leader, do you know these 6 skills?
[200 opencv routines] 209 Color image segmentation in HSV color space
leetcode:45. 跳跃游戏 II【经典贪心】
How to grab the mobile phone bag for analysis? Fiddler artifact may help you!
60 个神级 VS Code 插件!!
Collective search + drawing creation
60 divine vs Code plug-ins!!
权限想要细化到按钮,怎么做?
socket(2)