天天看點

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

  我現在已經厭倦了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) 不為空

* @return 如果collection不等于null且size>0,傳回true;否則,傳回false。

public static boolean notempty(collection<?> collection) {

return !isempty(collection);

* 判斷map不為空

* @return 如果map不為 null且size>0,傳回true;否則,傳回false。

public static boolean notempty(map<?, ?> map) {

return !isempty(map);

* 判斷一個數組不為空。

* @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;

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));

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));

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));

public static void testcollectionnotempty() {

boolean listwithpositivesize = emptyutils.notempty(list);

asserttrue(listwithpositivesize);

boolean listwithzerosize = emptyutils.notempty(emptylist);

assertfalse(listwithzerosize);

boolean nullempty = emptyutils.notempty(nulllist);

assertfalse(nullempty);

boolean setwithpositivesize = emptyutils.notempty(set);

asserttrue(setwithpositivesize);

assertfalse(emptyutils.notempty(nullset));

assertfalse(emptyutils.notempty(emptyset));

public static void testmapnotempty() {

asserttrue(emptyutils.notempty(map));

assertfalse(emptyutils.notempty(nullempty));

assertfalse(emptyutils.notempty(emptymap));

public static void testobjectarraynotempty() {

asserttrue(emptyutils.notempty(array));

assertfalse(emptyutils.notempty(nullarray));

assertfalse(emptyutils.notempty(emptyarray));

最新内容請見作者的github頁:http://qaseven.github.io/

繼續閱讀