当前位置:网站首页>Automatically generate test modules using JUnit4 and JUnitGenerator V2.0 in IDEA
Automatically generate test modules using JUnit4 and JUnitGenerator V2.0 in IDEA
2022-07-30 21:05:00 【Carrot rabbit】
Because of the needs of the project, I studied the automatic generation of test code.It will always be useful to record the experience.I personally think that it is better to have a good memory than to take more notes and more reflections and summaries.
1. Prerequisites
- Development environment is properly configured
- Project has resolved JUnit dependencies (pom.xml)
- I'm using version 4.12:
junit junit 4.12
2. JUnit configuration in IDEA
Open the Settings window and search for junit, as shown in the figure (both plugins are checked to add):

The JUnitGenerator V2.0 plugin can help us generate test code automatically.If the search junit does not have JUnitGenerator V2.0, operate as shown below (download and add):



Calling a template's method (Alt+Insert) tests all methods by default.If you want dynamic personalized generation, you can use the shortcut operation Ctrl + Shift + T on the class page to be tested, as shown in the following figure to personalize the settings:


It is now possible to run '***test class name**' on this class via the right-click menu, or via Run → Edit Configurations.

3. JUnit common assertions and annotations
JUnit provides us with some helper functions that help us determine whether the method under test is working as expected. Usually, these helper functions are called assertions.
Assertion core method

Notes

The execution order of a test class unit test is:
@BeforeClass –> @Before –> @Test –> @After –> @AfterClassThe calling sequence of each test method is:
@Before –> @Test –> @AfterCode example:
public class JunitFlowTest {/* **1. The method modified by @BeforeClass will be executed before all methods are called,* and the method is static, so it will run when the test class is loaded,* And it will only have one instance in memory, which is more suitable for loading configuration files.* 2. The method modified by @AfterClass is usually used to clean up resources, such as closing the connection to the database* 3. @Before and @After will be executed once before and after each test method.* */@BeforeClasspublic static void setUpBeforeClass() throws Exception {System.out.println("this is beforeClass...");}@AfterClasspublic static void tearDownAfterClass() throws Exception {System.out.println("this is afterClass...");}@Beforepublic void setUp() throws Exception {System.out.println("this is before...");}/*** Even if an exception is thrown in the @Before annotated method and @Test annotated method,* All @After annotated methods will still be executed*/@Afterpublic void tearDown() throws Exception {System.out.println("this is after");}/* *** 1. Failure is generally caused by the failure of the assertion method used in unit testing,* This indicates that the test point found a problem* , which means that the output of the program is not what we expected.* 2. Errors are caused by code exceptions, which can arise from errors in the test code itself,* can also be in the code under test* a hidden bug* 3. Test cases are not used to prove that you are right, but to prove that you are not wrong.*/@Testpublic void testAdd() {assertEquals(5, new Calculate().add(3,3));}@Testpublic void testDivide() {assertEquals(3, new Calculate().divide(6, 0));}}边栏推荐
- 第04章 逻辑架构【1.MySQL架构篇】【MySQL高级】
- JSESSIONID description in cookie
- C语言中指针没那么难~ (1)【文章结尾有资料】
- 数字货币期货现货交易技巧,把握关键进场的买入点!(纯干货)
- Structured Streaming报错记录:Overloaded method foreachBatch with alternatives
- 弹性盒子模型
- Use the map function to operate on each element in the list It seems that you don't need a map
- Flink_CDC搭建及简单使用
- MySQL 高级(进阶) SQL 语句 (一)
- 关于SFML Rect.inl文件报错的问题
猜你喜欢

@WebServlet注解(Servlet注解)
![KEIL problem: [keil Error: failed to execute 'C:\Keil\ARM\ARMCC']](/img/24/762cec2b4495e80a3e45fac1fac13e.png)
KEIL problem: [keil Error: failed to execute 'C:\Keil\ARM\ARMCC']

Oblique document scanning and character recognition (opencv, coordinate transformation analysis)

IDEA2018.3.5 cancel double-click Shift shortcut

2021年PHP-Laravel面试题问卷题 答案记录

MySQL的Replace用法详解

【深度学习】对迁移学习中域适应的理解和3种技术的介绍

MySQL笔记2(函数,约束,多表查询,事务)

Network layer protocol------IP protocol

R package调试
随机推荐
nVisual网络可视化管理平台功能和价值点
MySQL笔记1(数据库的好处,数据库的概念,数据库的特点,MySQL的启动,数据模型,SQL)
是对称矩阵的对角化
2021年PHP-Laravel面试题问卷题 答案记录
MySQL的on duplicate key update 的使用
Outsourcing worked for three years, it was abolished...
Generate OOM records in a production environment. Conclusion: Don't be lazy to query useless fields unless you are completely sure.
QUALITY-GATED CONVOLUTIONAL LSTM FOR ENHANCING COMPRESSED VIDEO
Oblique document scanning and character recognition (opencv, coordinate transformation analysis)
Mysql8创建用户以及赋权操作
[Limited Time Bonus] 21-Day Learning Challenge - MySQL from entry to mastery
Mysql 回表
365天挑战LeetCode1000题——Day 044 按公因数计算最大组件大小 并查集
【深度学习】对迁移学习中域适应的理解和3种技术的介绍
KingbaseESV8R6 snapshot too old的配置和测试
文字的选择与排版
DPW-SDNet: Dual Pixel-Wavelet Domain Deep CNNsfor Soft Decoding of JPEG-Compressed Images
MySQL的 DDL和DML和DQL的基本语法
关于MySQL主从复制的数据同步延迟问题
Babbitt | Metaverse Daily Must Read: The shuffling is coming, will the digital Tibetan industry usher in a new batch of leaders in the second half?Will there be new ways to play?...