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

126. 单词接龙 II

【图像增强】基于Step和Polynomial 滤波实现图像增强附matlab代码

lvs+keepalived高可用负载均衡集群

Cyanine5tetrazine(CAS号:1427705-31-4)结构式原理

那些关于DOM的常见Hook封装(一)

01| 数据类型

书签收藏难整理?这款书签工具管理超方便

VMware不正常关机

Kubernetes:(十四)安全机制(一定要做好安全措施哦)

甲乙丙丁加工零件,加工的总数是370, 如果甲加工的零件数多10,如果乙加工的零件数少20,如果丙加工的 零件数乘以2,如果丁加工的零件数除以2,四个人的加工数量相等,求甲乙丙丁各自加工多少个零件?
随机推荐
Shell脚本:函数
深度学习——以天气识别为例,探讨如何保存神经网络模型
20220524搜索和排序:搜索二维矩阵II
多线程 (进阶+初阶)
C专家编程 第9章 再论数组 9.2 为什么会发生混淆
20220527动态规划:零钱兑换
Image.new() 及 img.paste() 的用法记录
unshift() :将一个或多个元素添加到数组的开头
C专家编程 第9章 再论数组 9.1 什么时候数组与指针相同
net core 读取sqlserver所有表转为json
flat() :递归地将数组展平到指定的深度
【图像增强】基于Step和Polynomial 滤波实现图像增强附matlab代码
hcip MPLS 实验
并查集相关知识点
【洛谷】P2613 【模板】有理数取余
Matlab optimization method -- 0.618 method
Building PO layered architecture of automated testing framework from 0
机器学习入门
第一部分:和数组相关的问题
让历史文化“活”起来,北京河图“万象中轴”助力打造北京城市金名片