当前位置:网站首页>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 –> @AfterClass
The calling sequence of each test method is:
@Before –> @Test –> @After
Code 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));}}
边栏推荐
- 基于Apache Doris的湖仓分析
- PPT如何开启演讲者模式?PPT开启演讲者模式的方法
- @Transactional注解在类上还是接口上使用,哪种方式更好?
- Redis数据更新,是先更新数据库还是先更新缓存?
- 三层架构简单配置
- Simple configuration of three-tier architecture
- Oblique document scanning and character recognition (opencv, coordinate transformation analysis)
- Image Restoration by Estimating Frequency Distribution of Local Patches
- MySQL8重置root账户密码图文教程
- Mysql——字符串函数
猜你喜欢
【考研词汇训练营】Day18 —— amount,max,consider,account,actual,eliminate,letter,significant,embarrass,collapse
【Codeforces思维题】20220728
QUALITY-GATED CONVOLUTIONAL LSTM FOR ENHANCING COMPRESSED VIDEO
MySQL——几种常见的嵌套查询
JSESSIONID description in cookie
CDH集群spark-shell执行过程分析
MVC模式和三层架构
HMS Core音频编辑服务音源分离与空间音频渲染,助力快速进入3D音频的世界
深度学习模型训练前的必做工作:总览模型信息
Cookie中的JSESSIONID说明
随机推荐
《快速掌握QML》第六章 动画
HJ85 最长回文子串
MySQL_关于JSON数据的查询
Image Restoration by Estimating Frequency Distribution of Local Patches
想要写出好的测试用例,先要学会测试设计
MySQL 删除表数据,重置自增 id 为 0 的两个方式
Mysql8创建用户以及赋权操作
MySQL Workbench 安装及使用
MySQL 视图(详解)
idea plugins搜索不到插件
chrome extension: how to make the dialog be on the right side of the current window?
HarmonyOS Notes ------------- (3)
@Transactional注解在类上还是接口上使用,哪种方式更好?
mpls简介
【深度学习】目标检测|SSD原理与实现
Redis数据更新,是先更新数据库还是先更新缓存?
Android Studio 实现登录注册-源代码 (连接MySql数据库)
MySQL——几种常见的嵌套查询
关于SFML Rect.inl文件报错的问题
2.网络资源访问工具:requests