当前位置:网站首页>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)
边栏推荐
- 【洛谷】P1082 同余方程
- DSPE-PEG-OH,DSPE-PEG-Hydroxyl,磷脂-聚乙二醇-羟基仅供科研实验使用
- Second data CEO CAI data warming invited to jointly organize the acceleration data elements online salon
- C专家编程 第10章 再论指针 10.2 指针数组就是Iliffle向量
- SwiftUI * Grid
- SQLserver重新累计问题
- C专家编程 第9章 再论数组 9.7 轻松一下---软件/硬件平衡
- 数组与切片
- 嵌入式系统驱动高级【2】——平台总线式驱动开发上_基础框架
- Kubernetes:(十三)secret与configmap的那些事
猜你喜欢
随机推荐
7月更新速递 | 产品实验室N+1,EasyV For Unreal上线!
Doris从理论详解到千万级数据量场景使用
对线面试官实现去重和幂等
C语言力扣第56题之合并区间。排序+双指针
【图像去噪】基于边缘增强扩散 (cEED) 和 Coherence Enhancing Diffusion (cCED) 滤波器实现图像去噪附matlab代码
powershell execution strategy
SQLserver重新累计问题
关于eBPF与可观测性,你想知道的都在这里
C专家编程 第10章 再论指针 10.2 指针数组就是Iliffle向量
全文翻译:Multimodal Neural Networks: RGB-D for Segmantic Segmentation and Object Detection
C专家编程 第8章 为什么程序员无法分清万圣节和圣诞节 8.10 轻松一下---国际C语言混乱代码大赛
What are the functions and applications of the smart counter control board?
嵌入式系统驱动高级【2】——平台总线式驱动开发上_基础框架
Exchange VLAN experiment
图论相关知识
书签收藏难整理?这款书签工具管理超方便
ReentrantLock源码分析
甲乙丙丁加工零件,加工的总数是370, 如果甲加工的零件数多10,如果乙加工的零件数少20,如果丙加工的 零件数乘以2,如果丁加工的零件数除以2,四个人的加工数量相等,求甲乙丙丁各自加工多少个零件?
JavsScript系列-Promise的错误捕获
新型双功能螯合剂NOTA及其衍生物CAS号:147597-66-8p-SCN-Bn-NOTA









