当前位置:网站首页>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));}}边栏推荐
猜你喜欢

HMS Core音频编辑服务音源分离与空间音频渲染,助力快速进入3D音频的世界

KingbaseES V8R6备份恢复案例之---同一数据库创建不同stanza备份

MySQL BIGINT 数据类型

flowable工作流所有业务概念

GPGGA NTRIP RTCM Notes

MySQL的Replace用法详解

Office365无法打开word文档怎么办?Office365无法打开word文档的解决方法

用于命名实体识别的模块化交互网络

《快速掌握QML》第六章 动画

Why do so many people who teach themselves software testing give up later...
随机推荐
MySQL 多表关联一对多查询实现取最新一条数据
MySQL group_concat()详解
【网络安全专栏目录】--企鹅专栏导航
@RequestParam使用
CDH集群spark-shell执行过程分析
chrome扩展:如何使对话框位于当前窗口的右侧?
mysql 递归函数with recursive的用法
MySQL 删除表数据,重置自增 id 为 0 的两个方式
数据指标口径不统一、重复开发?亿信ABI指标管理平台帮你解决
肖特基二极管厂家ASEMI带你认识电路中的三大重要元器件
MySQL——几种常见的嵌套查询
flowable工作流所有业务概念
【信息安全技术】RSA算法的研究及不同优化策略的比较
Mysql 回表
【luogu P8031】Kućice(计算几何)
MVC模式和三层架构
Image Restoration by Estimating Frequency Distribution of Local Patches
什么是公约数
[Typora] This beta version of Typora is expired, please download and install a newer version.
Office365无法打开word文档怎么办?Office365无法打开word文档的解决方法