当前位置:网站首页>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 …
边栏推荐
- Balanced binary search tree
- Servlet详解
- 关于自动控制原理资料更新
- TKKC round#3
- Introduction, installation and use of postman tool
- TCP RTT measurement tips
- Yida technology signed a contract with seven wolves to help the digital transformation of "Chinese men's wear leader"
- Detailed explanation of agency mode
- MySQL gets fields and comments by indicating
- Mysql 通过表明获取字段以及注释
猜你喜欢

网上立案流程

The process from troubleshooting to problem solving: the browser suddenly failed to access the web page, error code: 0x80004005, and the final positioning: "when the computer turns on the hotspot, the

Uncover the secret of station B. is it true that programmers wear women's clothes and knock code more efficiently?

60 个神级 VS Code 插件!!

socket(1)

Balanced binary search tree

好想送对象一束花呀

Huada 04A operating mode / low power consumption mode

Genesis公链与美国一众加密投资者齐聚Consensus 2022

Industrial development status of virtual human
随机推荐
Flutter: Unsupported value: false/true
磁盤的結構
Stl+ tree
如何提取网页中的日期?
Shutter precautions for using typedef
直播软件app开发,左右自动滑动的轮播图广告
[untitled]
嵌入式开发:技巧和窍门——干净地从引导加载程序跳转到应用程序代码
AQS源码分析
软件设计的七大原则
Reduce the pip to the specified version (upgrade the PIP through CMP and reduce it to the original version)
如何抓手机的包进行分析,Fiddler神器或许能帮到您!
刷题笔记(十八)--二叉树:公共祖先问题
socket(2)
故障安全移动面板KTP900F Mobile下载程序提示无法下载,目标设备正在运行或未处于传输模式的解决办法
Opengauss kernel: simple query execution
Mysql 通过表明获取字段以及注释
TCP RTT测量妙计
Double linked list implementation
Machine learning: linear regression