qileilove

          blog已經(jīng)轉(zhuǎn)移至github,大家請訪問 http://qaseven.github.io/

          JUnit單元測試實踐:測試工具類和方法

            工作中,為了提高Web開發(fā)的質(zhì)量和效率,近期又為了保證自己的工具類等一系列可復用組件的質(zhì)量,我煞費苦心地開始認真學習和撰寫單元測試用例。
            我現(xiàn)在已經(jīng)厭倦了Debug程序,更討厭Debug Web程序,太浪費時間了。
            最近,線上的一個BM項目,出了個bug。浮點數(shù)相減,沒有判斷null,搞的我加班到9:30。
            苦逼的碼農(nóng)啊。
            下面,分享我的一個工具類和對應的單元測試用例。
            有不對的地方,還望能告知我。大家共同進步。
          /**
          * 判斷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
          *            鍵值對數(shù)據(jù)類型
          * @return 如果map是 null或size=0,返回true;否則,返回false。
          */
          public static boolean isEmpty(Map<?, ?> map) {
          return map == null || map.size() == 0;
          }
          /**
          * 判斷一個數(shù)組是否為空。
          *
          * @param array
          *            對象數(shù)組
          * @return 如果數(shù)組為null或者數(shù)組元素個數(shù)為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
          *            鍵值對數(shù)據(jù)類型
          * @return 如果map不為 null且size>0,返回true;否則,返回false。
          */
          public static boolean notEmpty(Map<?, ?> map) {
          return !isEmpty(map);
          }
          /**
          * 判斷一個數(shù)組不為空。
          *
          * @param array
          *            對象數(shù)組
          * @return 如果數(shù)組為null或者數(shù)組元素個數(shù)為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));
          }
          }

          posted on 2013-11-13 10:25 順其自然EVO 閱讀(321) 評論(0)  編輯  收藏 所屬分類: jmeter and badboy

          <2013年11月>
          272829303112
          3456789
          10111213141516
          17181920212223
          24252627282930
          1234567

          導航

          統(tǒng)計

          常用鏈接

          留言簿(55)

          隨筆分類

          隨筆檔案

          文章分類

          文章檔案

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 静乐县| 六枝特区| 二手房| 吕梁市| 临汾市| 黄陵县| 肇州县| 兖州市| 根河市| 锡林浩特市| 鹤庆县| 西乌珠穆沁旗| 襄城县| 峨边| 东丽区| 固安县| 太原市| 凤庆县| 博白县| 昌都县| 中山市| 延津县| 石柱| 尉犁县| 留坝县| 滁州市| 鸡东县| 海晏县| 内乡县| 惠水县| 东兴市| 东港市| 定安县| 华池县| 隆尧县| 阳信县| 绥阳县| 宜宾市| 哈密市| 无极县| 泽普县|