JUnit单元测试实践:测试工具类和方法(EmptyUtils)
以前的时候(读大学时),我认为写单元测试太费事了。现在,我改变看法了。
工作中,为了提高Web开发的质量和效率,近期又为了保证自己的工具类等一系列可复用组件的质量,我煞费苦心地开始认真学习和撰写单元测试用例。
我现在已经厌倦了Debug程序,更讨厌Debug Web程序,太浪费时间了。
最近,线上的一个BM项目,出了个bug。浮点数相减,没有判断null,搞的我加班到9:30。
苦逼的码农啊。
下面,分享我的一个工具类和对应的单元测试用例。
有不对的地方,还望能告知我。大家共同进步。
/**
* 判断Collection(List和Set),Map等集合类型是否为空,是否含有空值。
* 判断String是否为空,参考ApacheCommonsLang-StringUtils。
*
* @author leiwen
*/
public class EmptyUtils {
/**
* 判断Collection(List和Set) 是否为空
*
* @param collection
* List或Set类型的集合
* @return 如果collection是 null或size=0,返回true;否则,返回false。
*/
public static boolean isEmpty(Collection<?> collection) {
return collection == null || collection.size() == 0;
}
/**
* 判断map是否为空
*
* @param map
* 键值对数据类型
* @return 如果map是 null或size=0,返回true;否则,返回false。
*/
public static boolean isEmpty(Map<?, ?> map) {
return map == null || map.size() == 0;
}
/**
* 判断一个数组是否为空。
*
* @param array
* 对象数组
* @return 如果数组为null或者数组元素个数为0,返回true;否则,返回false。
*/
public static boolean isEmpty(Object[] array) {
return array == null || array.length == 0;
}
/**
* 判断Collection(List和Set) 不为空
*
* @param collection
* List或Set类型的集合
* @return 如果collection不等于null且size>0,返回true;否则,返回false。
*/
public static boolean notEmpty(Collection<?> collection) {
return !isEmpty(collection);
}
/**
* 判断map不为空
*
* @param map
* 键值对数据类型
* @return 如果map不为 null且size>0,返回true;否则,返回false。
*/
public static boolean notEmpty(Map<?, ?> map) {
return !isEmpty(map);
}
/**
* 判断一个数组不为空。
*
* @param array
* 对象数组
* @return 如果数组为null或者数组元素个数为0,返回false;否则,返回true。
*/
public static boolean notEmpty(Object[] array) {
return !isEmpty(array);
}
}
package cn.fansunion.webcommon.platform;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import junit.framework.TestCase;
import org.junit.Test;
import cn.fansunion.common.util.EmptyUtils;
/**
*
*
* @author leiwen
*/
public class EmptyUtilsTest extends TestCase {
@Test
public static void testCollectionIsEmpty() {
List<Integer> list = Arrays.asList(1, 2, 3);
boolean listWithPositiveSize = EmptyUtils.isEmpty(list);
assertFalse(listWithPositiveSize);
List<Integer> emptyList = new ArrayList<Integer>();
boolean listWithZeroSize = EmptyUtils.isEmpty(emptyList);
assertTrue(listWithZeroSize);
List<Integer> nullList = null;
boolean nullEmpty = EmptyUtils.isEmpty(nullList);
assertTrue(nullEmpty);
Set<Integer> set = new HashSet<Integer>();
set.add(100);
boolean setWithPositiveSize = EmptyUtils.isEmpty(set);
assertFalse(setWithPositiveSize);
Set<Integer> nullSet = null;
assertTrue(EmptyUtils.isEmpty(nullSet));
Set<Integer> emptySet = new HashSet<Integer>();
assertTrue(EmptyUtils.isEmpty(emptySet));
}
@Test
public static void testMapIsEmpty() {
Map<String, Object> map = new HashMap<String, Object>();
map.put("mapTest", "mapTestValue");
assertFalse(EmptyUtils.isEmpty(map));
Map<String, Object> nullEmpty = null;
assertTrue(EmptyUtils.isEmpty(nullEmpty));
Map<String, Object> emptyMap = new HashMap<String, Object>();
assertTrue(EmptyUtils.isEmpty(emptyMap));
}
@Test
public static void testObjectArrayIsEmpty() {
Integer[] array = { 1, 2, 3 };
assertFalse(EmptyUtils.isEmpty(array));
Integer[] nullArray = null;
assertTrue(EmptyUtils.isEmpty(nullArray));
Integer[] emptyArray = {};
assertTrue(EmptyUtils.isEmpty(emptyArray));
}
@Test
public static void testCollectionNotEmpty() {
List<Integer> list = Arrays.asList(1, 2, 3);
boolean listWithPositiveSize = EmptyUtils.notEmpty(list);
assertTrue(listWithPositiveSize);
List<Integer> emptyList = new ArrayList<Integer>();
boolean listWithZeroSize = EmptyUtils.notEmpty(emptyList);
assertFalse(listWithZeroSize);
List<Integer> nullList = null;
boolean nullEmpty = EmptyUtils.notEmpty(nullList);
assertFalse(nullEmpty);
Set<Integer> set = new HashSet<Integer>();
set.add(100);
boolean setWithPositiveSize = EmptyUtils.notEmpty(set);
assertTrue(setWithPositiveSize);
Set<Integer> nullSet = null;
assertFalse(EmptyUtils.notEmpty(nullSet));
Set<Integer> emptySet = new HashSet<Integer>();
assertFalse(EmptyUtils.notEmpty(emptySet));
}
@Test
public static void testMapNotEmpty() {
Map<String, Object> map = new HashMap<String, Object>();
map.put("mapTest", "mapTestValue");
assertTrue(EmptyUtils.notEmpty(map));
Map<String, Object> nullEmpty = null;
assertFalse(EmptyUtils.notEmpty(nullEmpty));
Map<String, Object> emptyMap = new HashMap<String, Object>();
assertFalse(EmptyUtils.notEmpty(emptyMap));
}
@Test
public static void testObjectArrayNotEmpty() {
Integer[] array = { 1, 2, 3 };
assertTrue(EmptyUtils.notEmpty(array));
Integer[] nullArray = null;
assertFalse(EmptyUtils.notEmpty(nullArray));
Integer[] emptyArray = {};
assertFalse(EmptyUtils.notEmpty(emptyArray));
}
}
原文链接:http://FansUnion.cn/articles/2271
JUnit单元测试实践:测试工具类和方法(EmptyUtils)的更多相关文章
- 基于Python的XSS测试工具XSStrike使用方法
基于Python的XSS测试工具XSStrike使用方法 简介 XSStrike 是一款用于探测并利用XSS漏洞的脚本 XSStrike目前所提供的产品特性: 对参数进行模糊测试之后构建合适的payl ...
- 大数据学习--day07(冒泡排序、Arrays工具类、方法可变参数)
冒泡排序.Arrays工具类.方法可变参数 冒泡排序之前写过,略 Arrays工具类 二分法查询 public static int binarySearch(int[] a,int key) 头信息 ...
- 【Android 工具类】经常使用工具类(方法)大全
收集经常使用的工具类或者方法: 1.获取手机分辨率 /** * 获取手机分辨率 */ public static String getDisplayMetrix(Context context) { ...
- OpenJDK源码研究笔记(四)-编写和组织可复用的工具类和方法
本篇主要讲解java.util.Arrays这个针对数组的工具类. 1.可复用的工具类和方法. 这个工具类里,包含很多针对数组的工具方法,如 排序.交换.二分查找.比较.填充.复制.hashcode ...
- Hutool中那些常用的工具类和方法
Hutool中那些常用的工具类和方法 Hutool是一个Java工具包,它帮助我们简化每一行代码,避免重复造轮子.如果你有需要用到某些工具方法的时候,不妨在Hutool里面找找,可能就有.本文将对Hu ...
- Junit单元测试数据生成工具类
在Junit单元测试中,经常需要对一些领域模型的属性赋值,以便传递给业务类测试,常见的场景如下: com.enation.javashop.Goods goods = new com.enation. ...
- spring && Cobertura && maven &&junit 单元测试以及测试覆盖率
1. 目的: junit 单元测试,Cobertura 测试覆盖率报告 项目目录结构 2. maven 配置 <project xmlns= ...
- Java基础知识强化63:Arrays工具类之方法源码解析
1. Arrays工具类的sort方法: public static void sort(int[] a): 底层是快速排序,知道就可以了,用空看. 2. Arrays工具类的toString方法底层 ...
- Collections 工具类和 Arrays 工具类常见方法
Collections Collections 工具类常用方法: 排序 查找,替换操作 同步控制(不推荐,需要线程安全的集合类型时请考虑使用 JUC 包下的并发集合) 排序操作 void revers ...
随机推荐
- 使用Django框架实现游戏站点搭建
完整project链接点击打开链接 上一篇中我们使用了Javascript和Html5实现了弹球游戏.而在本文中我们希望以其为基础实现游戏站点,可以实现用户的注冊登录.游戏成绩记录,排名显示.微博分享 ...
- android SearchView 样式修改
try { Class<?> argClass=mSearchView.getClass(); //指定某个私有属性 Field mSearchHintIconField = argCla ...
- bin/sh^M:损坏的解释器: 没有那个文件或目录
脚本文件保存时使用了DOS格式,用DOS2UNIX转为UNIX格式,也可以用vim打开,用:set ff=unix转换.不要在 Windows下编辑脚本文件,否则经常会遇到这种问题. 代码:sed - ...
- caioj1495: [视频]基于连通性状态压缩的动态规划问题:Formula 2
本来想写一天插头的,但是这题太难受(绝望)500+的代码量..我选择下午放松一下. 先ORZ一下苏大佬(yz的cdq啊%%%%%)居然把cdq论文里面的题抠出来出数据放在c站(呵呵真是个悲伤的故事不过 ...
- 使用poi读取word2007(.docx)中的复杂表格
使用poi读取word2007(.docx)中的复杂表格 最近工作需要做一个读取word(.docx)中的表格,并以html形式输出.经过上网查询,使用了poi. 对于2007及之后的word文档,需 ...
- Python入门 来点栗子
查天气(1) http://wthrcdn.etouch.cn/weather_mini?citykey=101280804 http://wthrcdn.etouch.cn/WeatherApi?c ...
- PCB MS SQL 小写转大写
由于SQL Server允许为小写进入 ,导致数据库中存在小写,在数据集成到MES或ERP时报错,Oracle要求大写导致, 需转换为大写,可通过以下语句,查询所有小写数据,再更新.
- 动态title
<html><head><meta charset="uft8"><title>测试title</title></ ...
- 阿里云centos系统上安装ftp
最近需要在一台阿里云的云服务器上搭建FTP服务器,在这篇博文中分享一下我们根据实际需求进行的一些配置. ftp软件用的是vsftpd. vsftpd是一款在Linux发行版中最受推崇的FTP服务器程序 ...
- Rancher 2:添加 NFS client provisioner 动态提供 Kubernetes 后端存储卷
一.前提说明 1.说明: NFS client provisioner 利用 NFS Server 给 Kubernetes 作为持久存储的后端,并且动态提供PV. 默认 rancher 2 的存储类 ...