以前的时候(读大学时),我认为写单元测试太费事了。现在,我改变看法了。

工作中,为了提高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)的更多相关文章

  1. 基于Python的XSS测试工具XSStrike使用方法

    基于Python的XSS测试工具XSStrike使用方法 简介 XSStrike 是一款用于探测并利用XSS漏洞的脚本 XSStrike目前所提供的产品特性: 对参数进行模糊测试之后构建合适的payl ...

  2. 大数据学习--day07(冒泡排序、Arrays工具类、方法可变参数)

    冒泡排序.Arrays工具类.方法可变参数 冒泡排序之前写过,略 Arrays工具类 二分法查询 public static int binarySearch(int[] a,int key) 头信息 ...

  3. 【Android 工具类】经常使用工具类(方法)大全

    收集经常使用的工具类或者方法: 1.获取手机分辨率 /** * 获取手机分辨率 */ public static String getDisplayMetrix(Context context) { ...

  4. OpenJDK源码研究笔记(四)-编写和组织可复用的工具类和方法

    本篇主要讲解java.util.Arrays这个针对数组的工具类. 1.可复用的工具类和方法.  这个工具类里,包含很多针对数组的工具方法,如 排序.交换.二分查找.比较.填充.复制.hashcode ...

  5. Hutool中那些常用的工具类和方法

    Hutool中那些常用的工具类和方法 Hutool是一个Java工具包,它帮助我们简化每一行代码,避免重复造轮子.如果你有需要用到某些工具方法的时候,不妨在Hutool里面找找,可能就有.本文将对Hu ...

  6. Junit单元测试数据生成工具类

    在Junit单元测试中,经常需要对一些领域模型的属性赋值,以便传递给业务类测试,常见的场景如下: com.enation.javashop.Goods goods = new com.enation. ...

  7. spring && Cobertura && maven &&junit 单元测试以及测试覆盖率

    1. 目的:       junit 单元测试,Cobertura   测试覆盖率报告       项目目录结构          2. maven 配置     <project xmlns= ...

  8. Java基础知识强化63:Arrays工具类之方法源码解析

    1. Arrays工具类的sort方法: public static void sort(int[] a): 底层是快速排序,知道就可以了,用空看. 2. Arrays工具类的toString方法底层 ...

  9. Collections 工具类和 Arrays 工具类常见方法

    Collections Collections 工具类常用方法: 排序 查找,替换操作 同步控制(不推荐,需要线程安全的集合类型时请考虑使用 JUC 包下的并发集合) 排序操作 void revers ...

随机推荐

  1. Android给定坐标计算距离

    给定两点的经纬度.计算两点之间的距离.这里要注意经纬度一定要依照顺序填写 1. 利用android中的工具获得,单位是米 float[] results=new float[1]; Location. ...

  2. 转:Java阳历转农历

    package cloud.app.prod.home.utils; import java.text.ParseException; import java.text.SimpleDateForma ...

  3. P3387 【模板】缩点 tarjan

    虽说是模板题,但是竟然中间有dp的部分...先tarjan缩点,重新建图.然后记忆化搜索,搜索dag中的最小环. 题干: 题目背景 缩点+DP 题目描述 给定一个n个点m条边有向图,每个点有一个权值, ...

  4. P2932 [USACO09JAN]地震造成的破坏Earthquake Damage 爆搜

    这题怎么这么水~~~本来以为挺难的一道题,结果随便一写就过了...本来还不知道损坏的牛棚算不算,结果不明不白就过了... 题干: 农夫John的农场遭受了一场地震.有一些牛棚遭到了损坏,但幸运地,所有 ...

  5. E20170906-mk

    portrait   n. 肖像,肖像画; 模型,标本; 半身雕塑像; 人物描写; orientation  n. 方向,定位,取向,排列方向; 任职培训; (外交等的) 方针[态度]的确定; 环境判 ...

  6. alipay.trade.refund (统一收单交易退款接口)[支付宝退款]

    首页官网退款的api: https://doc.open.alipay.com/docs/api.htm?spm=a219a.7395905.0.0.UTBitT&docType=4& ...

  7. Visual Studio q启动卡顿

    在开发人员CMD下面执行 Devenv.exe /ResetSettings ,然后顺利打开 总发现vs2015经常把cpu给占满了,导致电脑卡的不要不要的.这是CodeLens引起的,因为装了VAs ...

  8. Stick ------ 剪枝神题

    这个是自己剪得 , 我感觉已经很不错了  但是不知道哪里出了问题  一直  超时 // 根据所给答案 和 题目要求 最直观的就可以有剪枝的地方 而且 剪枝剪得越早 就越省时省力 // 好的思路也可以省 ...

  9. 另一种压缩图片的方法---Machine learning 之 PCA(Principle Component Analysis)

    PCA最主要的用途是用来减少特征向量的数目,N个特征向量 减小到 K个特征向量.如果为了可视化,k可以使3 或者 2.这样可以加速算法的学习速度. PCA用来压缩图像同一有效. 具体方式以及原理在gi ...

  10. 树莓派-基于raspistill实现定时拍照

    raspistill 经过上一篇<<树莓派-安装摄像头模块>>之后 raspistill 是树莓派基于摄像头拍照命令 比如我要截取一张宽1024px,高768px,旋转180度 ...