当前位置:网站首页>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));}}边栏推荐
- Simple configuration of three-tier architecture
- 【回归预测-CNN预测】基于卷积神经网络CNN实现数据回归预测附matlab代码
- 【软件工程之美 - 专栏笔记】31 | 软件测试要为产品质量负责吗?
- 【信息安全技术】RSA算法的研究及不同优化策略的比较
- HMS Core音频编辑服务音源分离与空间音频渲染,助力快速进入3D音频的世界
- [Deep Learning] Target Detection | SSD Principle and Implementation
- ENS 表情包域名火了!是炒作还是机遇?
- Motion Tuned Spatio-temporal Quality Assessmentof Natural Videos
- 【回归预测-lssvm分类】基于最小二乘支持向量机lssvm实现数据分类代码
- MySQL (2)
猜你喜欢
随机推荐
DPW-SDNet: Dual Pixel-Wavelet Domain Deep CNNs for Soft Decoding of JPEG-Compressed Images
MySQL的 DDL和DML和DQL的基本语法
Flink_CDC搭建及简单使用
@WebServlet注解(Servlet注解)
2.网络资源访问工具:requests
MySQL 删除表数据,重置自增 id 为 0 的两个方式
手把手教你搭建一台永久运行的个人服务器
关于MySQL主从复制的数据同步延迟问题
nVisual网络可视化管理平台功能和价值点
化二次型为标准型
【网络安全专栏目录】--企鹅专栏导航
R package调试
Mysql——字符串函数
Oblique document scanning and character recognition (opencv, coordinate transformation analysis)
MySQL的Replace用法详解
[Deep Learning] Understanding of Domain Adaptation in Transfer Learning and Introduction of 3 Techniques
MySQL ODBC驱动简介
MySQL group_concat()详解
chrome扩展:如何使对话框位于当前窗口的右侧?
PPT如何开启演讲者模式?PPT开启演讲者模式的方法









