当前位置:网站首页>Dark horse programmer unit test: getting started with JUnit framework
Dark horse programmer unit test: getting started with JUnit framework
2022-07-20 10:31:00 【Easy to cause trouble】
Unit test overview
unit testing
∙ \bullet ∙ Unit testing is to write test code for the smallest functional unit ,java The smallest functional unit of a program is a method , therefore , Unit testing is for java Method testing , Then check the correctness of the method .
Junit Unit test framework
∙ \bullet ∙ Junit It's using Java Language implementation of unit testing framework , It's open source ,java Developers should learn and use Junit Write unit tests .
∙ \bullet ∙ Besides , be-all IDE Tools are integrated Junit, So that we can go straight to IDE Write and run Junit test .
Junit advantage
∙ \bullet ∙ Junit You can flexibly choose which test methods to perform , The test method can be executed with one key .
∙ \bullet ∙ Junit You can generate test reports for all methods .
∙ \bullet ∙ A method in the unit test failed , Tests that will not affect their test methods .
Quick start to unit testing
The goal is : The expected results of business methods using unit tests , A quick start to correctness testing .
analysis :
1: take Junit Of jar Package import into project
∙ \bullet ∙ IDEA Usually it's made up Junit frame , Generally, there is no need to import .
∙ \bullet ∙ If IDEA Not integrated well , You need to import it manually Junit Of jar Package to project .
2: Write test methods : The method must be public 、 No return value 、 Non static methods without parameters .
3: Use... On test methods @Test annotation : Note that this method is a test method .
4: Complete the expected correctness test of the tested method in the test method .
Junit Commonly used annotations (Junit4)
annotation | explain |
---|---|
@Test | The test method |
@Before | Used to modify instance methods , This method will be executed once before each test method is executed . |
@After | Used to modify instance methods , This method will be executed once after each test method is executed |
@BeforeClass | Used to decorate static methods |
@AfterClass | Used to decorate static methods |
∙ \bullet ∙ Method to start execution : Initialize the resource .
∙ \bullet ∙ The method after execution : Release resources .
Code
∙ \bullet ∙ Test class
package com.lp.d1_junit;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
/* * Test class * */
public class TestUserService {
/* * The test method * */
UserService userService=new UserService();
@Test
public void testLoginName(){
String rs= userService.loginName("admin","123456");
// Test the correctness of the expected results : Assertion .
Assert.assertEquals(" There may be an error logging in "," Landing successful ",rs);
}
@Test
public void testSelectName(){
userService.selectName();
}
@Before
public void before(){
System.out.println("____before Do it once _____");
}
@After
public void after(){
System.out.println("____after Do it once ____");
}
}
∙ \bullet ∙ Functional class
package com.lp.d1_junit;
public class UserService {
public String loginName(String loginName,String passWord){
if("admin".equals(loginName)&&"123456".equals(passWord)){
return " Landing successful ";
}else {
return " There is a problem with the user name or password ";
}
}
public void selectName(){
System.out.println(10/1);
System.out.println(" Query all user names successfully ~~");
}
}
边栏推荐
- DHCP service and configuration
- 重载(Overload) 与 重写(Override)的区别
- 【资源记录】如何用Pytorch的DataLoader加载自己的数据集
- [report] TF fast RCNN runs its own training data could not broadcast input array from shape (4) into shape (0)
- gocore-v2框架-API接口开发理念
- gocore-v2框架发布-集成脚手架能力
- DHCP原理与配置
- ceph-deploy配置ceph分布式集群
- Variable influence notes from UCB CS 285 Sergey Levine
- 栈模拟队列
猜你喜欢
随机推荐
ModuleNotFoundError: No module named 'gflags'
gocore-v2框架-脚手架生成项目结构介绍
二叉树
使用 gst-launch 小记
zabbix 自动发现并监控ceph osd
传输层协议
kvm虚拟机迁移到openstack环境,提示InvalidDiskInfo Disk info file is invalid qemu-img fai
阿里云微消息队列 MQTT
论文笔记:Look Back and Predict Forward in Image Captioning# Look Back and Predict Forward in Image Capti
CEPH detailed mon_ osd_ max_ split_ count
ModuleNotFoundError: No module named 'cv2'
Solution to unmount failure using umount command
RAID磁盘列阵
在线问题反馈模块实战(七):安装部署swagger2
黑马程序员多线程实现方式三
Dark horse programmer multithreading implementation mode II
系统安全和运用
单臂路由和VRRP
How to turn a computer without public IP in a LAN into a server accessible to the public network (SSH access as an example)
ZABBIX automatically discovers and monitors GPU