JUnit单元测试实践:测试工具类和方法

工作中,为了提高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));
}
}


最新内容请见作者的GitHub页:http://qaseven.github.io/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值