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

转义字符笔记记录

MVC模式和三层架构

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?...

外包干了三年,废了...

Image Restoration by Estimating Frequency Distribution of Local Patches

flyway的快速入门教程

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

什么是公约数

C language: detailed explanation of operators

【元胞自动机】基于元胞自动机模拟生命演化、病毒感染等实例附matlab代码
随机推荐
用于视频压缩伪影消除的深度卡尔曼滤波网络
bebel系列- 插件开发
Android studio连接MySQL并完成简单的登录注册功能
MySQL——几种常见的嵌套查询
2021年PHP-Laravel面试题问卷题 答案记录
chrome扩展:如何使对话框位于当前窗口的右侧?
@Transactional注解在类上还是接口上使用,哪种方式更好?
Oblique document scanning and character recognition (opencv, coordinate transformation analysis)
tcp协议传输中的粘包问题
PR视频剪辑软件教程
第04章 逻辑架构【1.MySQL架构篇】【MySQL高级】
KingbaseESV8R6 snapshot too old的配置和测试
Android Studio 实现登录注册-源代码 (连接MySql数据库)
网络安全实验环境搭建
[Deep Learning] Understanding of Domain Adaptation in Transfer Learning and Introduction of 3 Techniques
2022年SQL经典面试题总结(带解析)
[The Beauty of Software Engineering - Column Notes] 31 | Is software testing responsible for product quality?
MySql 创建索引
Office365无法打开word文档怎么办?Office365无法打开word文档的解决方法
三层架构简单配置