当前位置:网站首页>2022-08-08 The fifth group Gu Xiangquan study notes day31-collection-junit unit test
2022-08-08 The fifth group Gu Xiangquan study notes day31-collection-junit unit test
2022-08-09 03:06:00 【aggressive leeks】
JUnit单元测试
Junit是一个Java语言的单元测试框架
Junit单元测试的好处
- A series of test methods can be written,Unit test all interfaces or methods of the project
- 启动后,自动化的测试
- Just need to see the final result
- The use cases of each unit test are relatively independent,有Junit启动
- 添加,删除,Testing of shielding methods
jar包
If the introduction is a third-party plug-in,xxx.jar的文件.
First, import this file into our project directory
凄恻,To be added to the project's since directory
测试方法
- There can be no return pride
- 不能有参数列表
- 必须有Test注解
Junit断言
JunitAll assertions of Assert类中
This class provides many useful assertions to write test cases
Only invalid assertions are logged
asserEquals:Check that the two variables or lighting are balanced
assertTrue:检查条件是否为真
assertFalse:Check if the field is home
assertNot Null:检查对象是否不为空
assertNull:检查对象是否为空
Before:在测试方法之前执行
After:在测试方法之后执行
命名规范:
The naming of the unit test class,被测试类的类名,Test
测试方法的命名,test + The method name of the method being tested
Junit4、Junit5,建议使用4
测试ArrayList与LinkedList
Test tail insertion efficiency(尾插100w个元素)
package com.jsoft.junit;
import org.junit.Test;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
public class Demo1 {
// 测试ArrayListTail insertion element efficiency
@Test
public void testArrayList() {
long begin = System.currentTimeMillis();
List<Integer> list = new ArrayList<>(1000000);
long end = System.currentTimeMillis();
for (int i = 0; i < 1000000; i++) {
list.add(i);
}
System.out.println("耗时:" + (end - begin) + "ms");
}
// 测试LinkedListTail insertion element efficiency
@Test
public void testLinkedList() {
List<Integer> list = new LinkedList<>();
long begin = System.currentTimeMillis();
for (int i = 0; i < 1000000; i++) {
list.add(i);
}
long end = System.currentTimeMillis();
System.out.println("耗时:" + (end - begin) + "ms");
}
}
We tested the results three times,取平均值
ArrayList耗时:1ms (1ms、1ms、1ms)
LinkedList耗时:108ms (115ms、105ms、103ms)
Test the efficiency of random retrieval(对10wrandom retrieval of data10w次)
package com.jsoft.junit;
import org.junit.Test;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Random;
public class Demo1 {
// 测试ArrayListRandom retrieval element efficiency
@Test
public void testArrayList() {
// itit
Random r = new Random();
List<Integer> list = new ArrayList<>(100000);
for (int i = 0; i < 100000; i++) {
list.add(i);
}
// begin
long begin = System.currentTimeMillis();
for (int i = 0; i < list.size(); i++) {
list.get(r.nextInt(list.size()));
}
long end = System.currentTimeMillis();
System.out.println("耗时:" + (end - begin) + "ms");
}
// 测试LinkedListRandom retrieval element efficiency
@Test
public void testLinkedList() {
// itit
Random r = new Random();
List<Integer> list = new LinkedList<>();
for (int i = 0; i < 100000; i++) {
list.add(i);
}
// begin
long begin = System.currentTimeMillis();
for (int i = 0; i < list.size(); i++) {
list.get(r.nextInt(list.size()));
}
long end = System.currentTimeMillis();
System.out.println("耗时:" + (end - begin) + "ms");
}
}
We tested the results three times,取平均值
ArrayList耗时:ms (6ms、5ms、3ms)
LinkedList耗时:ms (5190ms、5158ms、5232ms)
边栏推荐
猜你喜欢
leetcode-23.合并K个升序链表
C专家编程 第9章 再论数组 9.6 C语言的多维数组
lvs+keepalived高可用负载均衡集群
【物理应用】基于El-centro地震波作用下隔震与非隔震支座下的顶层位移、速度、加速度的对比情况附matlab代码
DSP28379学习笔记 (一)——GPIO基本操作
LeetCode_43_字符串相乘
365 days challenge LeetCode1000 topic - Day 051 special binary sequence partition
一本通1258——数字金字塔(动态规划)
7月更新速递 | 产品实验室N+1,EasyV For Unreal上线!
Zabbix 5.0 监控教程(五)
随机推荐
第一部分:和数组相关的问题
What are the functions and applications of the smart counter control board?
Celery进阶_任务优先级分配
powershell execution strategy
【扫雷--1】
条件变量condition_variable实现线程同步
C专家编程 第9章 再论数组 9.6 C语言的多维数组
ARM开发(二)ARM体系结构——ARM,数据和指令类型,处理器工作模式,寄存器,状态寄存器,流水线,指令集,汇编小练习题
DSPE-PEG-OH,DSPE-PEG-Hydroxyl,磷脂-聚乙二醇-羟基仅供科研实验使用
【问题记录】pip 安装报错 Failed to establish a new connection
Talk about those marketing tools - coupons
马斯克被因狗狗币被索赔2580亿美元 操纵价格牟利?狗狗币已下跌92%
DSP28379学习笔记 (一)——GPIO基本操作
Leetcode刷题——148. 排序链表
20220528动态规划:最长递增子序列
Chapter3 numpy创建数组
phpStdudy的下载和DVWA的搭建
365天挑战LeetCode1000题——Day 051 特殊的二进制序列 分治
【图像增强】基于Step和Polynomial 滤波实现图像增强附matlab代码
pytorch 自定义dataset