当前位置:网站首页>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));}}
边栏推荐
- C language: detailed explanation of operators
- MySQL 高级(进阶) SQL 语句 (一)
- QUALITY-GATED CONVOLUTIONAL LSTM FOR ENHANCING COMPRESSED VIDEO
- Apache DolphinScheduler新一代分布式工作流任务调度平台实战-上
- HJ85 longest palindrome substring
- HMS Core音频编辑服务音源分离与空间音频渲染,助力快速进入3D音频的世界
- CDH集群spark-shell执行过程分析
- GPGGA NTRIP RTCM 笔记
- MySQL的on duplicate key update 的使用
- 6.3有定型性 第七章
猜你喜欢
Outsourcing worked for three years, it was abolished...
MySQL ODBC驱动简介
7、MySQL Workbench 导出导入数据库
为什么那么多自学软件测试的人,后来都放弃了...
ENS 表情包域名火了!是炒作还是机遇?
Use the map function to operate on each element in the list It seems that you don't need a map
Mysql——字符串函数
Flex布局详解
【回归预测-lssvm分类】基于最小二乘支持向量机lssvm实现数据分类代码
《快速掌握QML》第六章 动画
随机推荐
MySQL group_concat()详解
Structured Streaming报错记录:Overloaded method foreachBatch with alternatives
深度学习模型训练前的必做工作:总览模型信息
MySQL的 DDL和DML和DQL的基本语法
mpls简介
What is the common factor
MySQL的主从复制
Running the evict task with compensationTime
2021年PHP-Laravel面试题问卷题 答案记录
Apache DolphinScheduler新一代分布式工作流任务调度平台实战-上
什么是公约数
这本记述40年前历史的游戏书,预言的却是当下的事
HMS Core音频编辑服务音源分离与空间音频渲染,助力快速进入3D音频的世界
MySQL 多表关联一对多查询实现取最新一条数据
MySQL 视图(详解)
Flink_CDC搭建及简单使用
MySQL 删除表数据,重置自增 id 为 0 的两个方式
@WebServlet注解(Servlet注解)
MVC模式和三层架构
文字的选择与排版