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

工作中,为了提高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. C++开发人脸性别识别总结

    历时一个月,最终在昨天把<C++开发人脸性别识别总结>系列博客完毕了,第一篇博客发表在2015年12月29日,截止昨天2016年2月29日最后一篇完毕,去除中间一个月的寒假,正好一个月,首 ...

  2. Myeclipse中解决spring配置文件无提示问题

    相信非常多人都遇到过在部署spring框架写spring的配置文件时无提示内容的问题,都是仅仅能提示一些标签 名,而无法提示属性值,bz我本人今天也遇到了这种问题.在网上找了非常久答案,非常多方法都不 ...

  3. 使用memcachedclientXmemcached与Spring整合

    1 简单介绍 Xmemcached是一个高性能的基于java nio的memcachedclient.在经过三个RC版本号后.正式公布1.10-final版本号. xmemcached特性一览: 1. ...

  4. jsp模板配置

    <%-- Created by IntelliJ IDEA. User: ${USER} Date: ${DATE} Time: ${TIME} To change this template ...

  5. 杂项-人物:Alan cooper

    ylbtech-杂项-人物:Alan cooper Alan Cooper ,“VB之父”“交互设计之父”,荣获视窗先锋奖(Microsoft Windows Pioneer)和软件梦幻奖(Softw ...

  6. 1tb等于多少g 1TB和500G有什么区别

    转自:http://www.a207.com/article/view_39392 移动硬盘.U盘是生活中常见的用品,他们的内存大小是什么标准.很多人对于1tb等于多少g和1tb和500g有什么区别不 ...

  7. vim gvim技巧大全(9)(转载)

    vim gvim技巧大全(9) 2 用命令}移动到这个段落的底部,标记为b3 输入命令:'a,'b move来移动文本.老版本的Vi编辑器不能很好的来处理多文件.但是Vim在处理多文件上却显得优秀得多 ...

  8. 一、SQL系列之~使用SQL语言导出数据及实现定时导出数据任务

    一般情况下,SQL数据库中带有导入与导出数据的直接按键操作,点击数据表所在的数据库--任务--导出/导入数据,根据导入/导出向导直接将数据导出即可. 但导出的数据格式多为Excel格式,如果需要导出的 ...

  9. Zeppelin0.6.2+sparkR2.0.2环境搭建

    0.序 先吐槽一下网上旧版本的Zeppelin和R的安装,让我折腾了几个小时. 不过最终还是调通了也不容易,其实我现在一点R都没有学呢,只是刚看了一节课,但是这个工具既然出现在了Spark中,我想它还 ...

  10. 认识JDK、JRE、JVM

    JDK.JRE.JVM之间的关系: 首先看看JDK与JRE的区别与联系,如下图所示: 由图可知: JDK = JRE + Tools&Tool APIs JDK的核心是Java SE API. ...