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

工作中,为了提高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. jquery文件批量上传控件Uploadify3.2(java springMVC)

    人比較懒  有用为主 不怎么排版了 先放上Uploadify的官网链接:http://www.uploadify.com/  -->里面能够看到PHP的演示样例,属性说明,以及控件下载地址.分f ...

  2. SerializeUtil 序列化,反序列化工具类

    package cloud.app.prod.home.utils; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutp ...

  3. Java-java-com-util-common-service:CrudService.java

    ylbtech-Java-java-com-util-common-service:CrudService.java 1.返回顶部 1. package com.shineyoo.manager.ut ...

  4. Thinkpad E450c进入BIOS

    重启系统,一直按F12,进入系统设置后,按tab进入App Menu选项卡,选择Setup按回车进入BIOS设置

  5. DCOM 找不到 office word 的解决方法

    1. 在运行里面  输入     comexp.msc -32  2.在“DCOM配置”中,为IIS账号配置操作Word(其他Office对象也一样)的权限. 具体操作:“组件服务(Component ...

  6. 7.Flask-上传文件和访问上传的文件

     1.1.上传文件和访问上传的文件 upload_file_demo.py from flask import Flask,request,render_template import os from ...

  7. [ller必读] LoveLive! 必备技能之 Python Pillow 自动处理截图

    起因 喜欢的歌,静静地听:喜欢的人,远远的看.30天前,就是3月14号,我情不自禁地走近了<LoveLive!学院偶像祭>,这是我的第一张卡片(见下图).第二天也就是3月15日,海未生日了 ...

  8. Windows-Server-2008、IIS7.0环境下配置伪静态化

    在Windows-Server-2008.IIS7.0环境下配置伪静态化                首先,是IIS7.0的配置,由于Windows Server 2008操作系统默认的IIS版本为 ...

  9. hibernate中的懒加载和急加载

    懒加载(FatchType.LAZY)是Hibernate为提高程序执行效率而提供的一种机制,简单说就是只有正真使用其属性的时候,数据库才会进行查询. 具体的执行过程就是:Hibernate从数据库获 ...

  10. <aop:aspectj-autoproxy />

    通过配置织入@Aspectj切面 虽然可以通过编程的方式织入切面,但是一般情况下,我们还是使用spring的配置自动完成创建代理织入切面的工作. 通过aop命名空间的<aop:aspectj-a ...