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/

相关文章
|
7月前
|
测试技术 开发者 Python
Python单元测试入门:3个核心断言方法,帮你快速定位代码bug
本文介绍Python单元测试基础,详解`unittest`框架中的三大核心断言方法:`assertEqual`验证值相等,`assertTrue`和`assertFalse`判断条件真假。通过实例演示其用法,帮助开发者自动化检测代码逻辑,提升测试效率与可靠性。
530 1
|
7月前
|
机器学习/深度学习 人工智能 自然语言处理
如何让AI更“聪明”?VLM模型的优化策略与测试方法全解析​
本文系统解析视觉语言模型(VLM)的核心机制、推理优化、评测方法与挑战。涵盖多模态对齐、KV Cache优化、性能测试及主流基准,助你全面掌握VLM技术前沿。建议点赞收藏,深入学习。
2285 8
|
10月前
|
测试技术
软考软件评测师——可靠性测试测试方法
软件可靠性是指软件在规定条件和时间内完成预定功能的能力,受运行环境、软件规模、内部结构、开发方法及可靠性投入等因素影响。失效概率指软件运行中出现失效的可能性,可靠度为不发生失效的概率,平均无失效时间(MTTF)体现软件可靠程度。案例分析显示,嵌入式软件需满足高可靠性要求,如机载软件的可靠度需达99.99%以上,通过定量指标评估其是否达标。
|
10月前
|
消息中间件 缓存 监控
性能测试怎么做?方法、流程与核心要点解析
本文系统阐述了性能测试的核心方法论、实施流程、问题定位优化及报告编写规范。涵盖五大测试类型(负载验证、极限压力、基准比对、持续稳定性、弹性扩展)与七项关键指标,详解各阶段任务如需求分析、场景设计和环境搭建,并提供常见瓶颈识别与优化实战案例。最后规范测试报告内容框架与数据可视化建议,为企业级实践提出建立基线库、自动化回归和全链路压测体系等建议,助力高效开展性能测试工作。
|
10月前
|
人工智能 Java 测试技术
Spring Boot 集成 JUnit 单元测试
本文介绍了在Spring Boot中使用JUnit 5进行单元测试的常用方法与技巧,包括添加依赖、编写测试类、使用@SpringBootTest参数、自动装配测试模块(如JSON、MVC、WebFlux、JDBC等),以及@MockBean和@SpyBean的应用。内容实用,适合Java开发者参考学习。
1121 0
|
6月前
|
Java 测试技术 数据库连接
【SpringBoot(四)】还不懂文件上传?JUnit使用?本文带你了解SpringBoot的文件上传、异常处理、组件注入等知识!并且带你领悟JUnit单元测试的使用!
Spring专栏第四章,本文带你上手 SpringBoot 的文件上传、异常处理、组件注入等功能 并且为你演示Junit5的基础上手体验
1076 4
|
XML Java 测试技术
Spring5入门到实战------17、Spring5新功能 --Nullable注解和函数式注册对象。整合JUnit5单元测试框架
这篇文章介绍了Spring5框架的三个新特性:支持@Nullable注解以明确方法返回、参数和属性值可以为空;引入函数式风格的GenericApplicationContext进行对象注册和管理;以及如何整合JUnit5进行单元测试,同时讨论了JUnit4与JUnit5的整合方法,并提出了关于配置文件加载的疑问。
Spring5入门到实战------17、Spring5新功能 --Nullable注解和函数式注册对象。整合JUnit5单元测试框架
|
Java 测试技术 Android开发
课时148:junit测试工具
课时148介绍了JUnit测试工具的使用,包括定义、配置和编写测试程序。JUnit是流行的用例测试工具,用于确保代码稳定性。
296 0
|
Java 程序员 测试技术
Java|让 JUnit4 测试类自动注入 logger 和被测 Service
本文介绍如何通过自定义 IDEA 的 JUnit4 Test Class 模板,实现生成测试类时自动注入 logger 和被测 Service。
309 5
下一篇
开通oss服务