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

发表于:2013-11-12 11:21  作者:zhao_perry   来源:51Testing软件测试网采编

字体: | 上一篇 | 下一篇 |我要投稿 | 推荐标签:

/**
* 判断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));
}
}

22/2<12

评 论

论坛新帖

顶部 底部


建议使用IE 6.0以上浏览器,800×600以上分辨率,法律顾问:上海信义律师事务所 项棋律师
版权所有 上海博为峰软件技术股份有限公司 Copyright©51testing.com 2003-2021, 沪ICP备05003035号
投诉及意见反馈:webmaster@51testing.com; 业务联系:service@51testing.com 021-64471599-8017

沪公网安备 31010102002173号

51Testing官方微信

51Testing官方微博

扫一扫 测试知识全知道