在我的博客文章中,Java越来越接受静态导入吗? ,我讨论了在Java中越来越多地使用静态导入来使代码在某些情况下更流畅。 Java 单元测试特别受静态导入的影响,在此博客文章中,我提供了一个简单的示例,说明如何使用静态导入来使用JUnit和Hamcrest进行更流畅的单元测试。

下一个代码清单是一个简单的IntegerArithmetic类,它具有一个需要进行单元测试的方法。

IntegerArithmetic.java

package dustin.examples;/*** Simple class supporting integer arithmetic.* * @author Dustin*/
public class IntegerArithmetic
{/*** Provide the product of the provided integers.* * @param integers Integers to be multiplied together for a product.* @return Product of the provided integers.* @throws ArithmeticException Thrown in my product is too small or too large*     to be properly represented by a Java integer.*/public int multipleIntegers(final int ... integers){int returnInt = 1;for (final int integer : integers){returnInt *= integer;}return returnInt;}
}

接下来显示测试上述方法的一个方面的一种通用方法。

/*** Test of multipleIntegers method, of class IntegerArithmetic, using standard* JUnit assertEquals.*/@Testpublic void testMultipleIntegersWithDefaultJUnitAssertEquals(){final int[] integers = {2, 3, 4 , 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15};final int expectedResult = 2 * 3 * 4 * 5 * 6 * 7 * 8 * 9 * 10 * 11 * 12 *13 * 14 * 15;final int result = this.instance.multipleIntegers(integers);assertEquals(expectedResult, result);}

在上面显示的相当典型的单元测试示例中,由于org.junit.Assert。*的静态导入(未显示),因此以流畅的方式调用了JUnit的assertEquals 。 但是,最新版本的JUnit( JUnit 4.4+ )已经开始包括Hamcrest核心匹配器,这可以进行更流畅的测试,如下面的代码片段所示。

/*** Test of multipleIntegers method, of class IntegerArithmetic, using core* Hamcrest matchers included with JUnit 4.x.*/@Testpublic void testMultipleIntegersWithJUnitHamcrestIs(){final int[] integers = {2, 3, 4 , 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15};final int expectedResult = 2 * 3 * 4 * 5 * 6 * 7 * 8 * 9 * 10 * 11 * 12 *13 * 14 * 15;final int result = this.instance.multipleIntegers(integers);assertThat(result, is(expectedResult));}

在此示例中,JUnit的assertThat (自JUnit 4.4起也可作为org.junit.Assert.*的静态导入的一部分)与随附的Hamcrest核心匹配器is()结合使用。 这当然是一个问题,但是我更喜欢第二种方法,因为它对我来说更具可读性。 断言某些东西(结果)比其他方法(预期的)似乎更具可读性和流利性。 记住使用assertEquals时先列出预期结果还是实际结果有时会很棘手,结合使用assertThat和is()可以减少我编写和读取测试时的工作。 欢迎减少工作量,尤其是乘以大量测试时。

参考:在Inspired by Actual Events博客上,我们的JCG合作伙伴 Dustin Marx 通过JUnit和Hamcrest改进了assertEquals 。

翻译自: https://www.javacodegeeks.com/2012/05/junit-and-hamcrest-improving-on.html

JUnit和Hamcrest:在assertEquals上进行改进相关推荐

  1. junit:junit_JUnit和Hamcrest:在assertEquals上进行改进

    junit:junit 在我的博客文章中,Java越来越接受静态导入吗? 在本文中,我讨论了在Java中越来越多地使用静态导入来使代码在某些情况下更加流畅. Java中的 单元测试特别受静态导入的影响 ...

  2. 软件测试实验——安装并使用junit、hamcrest和eclemma进行简单测试

    一.安装junit.hamcrest和eclemma 首先,到junit官网下载junit和hamcrest. 新建java项目 完整代码已经上传至github:flyzero的coding路 实验内 ...

  3. JUnit和hamcrest的jar包关系

    JUnit的jar包和hamcrest的jar包关系 junit和hamcrest是两个不同的框架,不同的东西.只不过是junit使用了hamcrest框架而已. 在junit上下载的junit的包解 ...

  4. [软件测试_LAB1]安装junit和hamcrest及其使用

    一.在IDE中集成junit和hamcrest 创建工程后,导入junit和hamcrest的jar包 hamcrest-all-1.3.jar junit-4.12.jar 使用的IDE为Intel ...

  5. 软件测试实验1:JUnit、Hamcrest、Eclemma

    前言: 1.JUnit是一个Java语言的单元测试框架,是程序员测试,即所谓白盒测试,因为程序员知道被测试的软件如何(How)完成功能和完成什么样(What)的功能.JUnit框架用一组assert方 ...

  6. junit集成Hamcrest测试集合中某个属性是否包含特定值

    junit已经集成Hamcrest但是还是需要引用hamcrest-library,不然只有基本方法,高级的没有 <dependency> <groupId>junit< ...

  7. junit、hamcrest、eclemma的安装与使用

    1.junit的安装与使用 1.1 安装步骤 1)从http://www.junit.org/ 下载junit相应的jar包: 2) 在CLASSPATH中加入JAR包所在的路径,如E:\Java\j ...

  8. 【目标检测】(6) YOLOV2 目标检测在V1基础上的改进

    各位同学好,今天和大家分享一下 YOLOV2 目标检测算法的原理,建议大家先学习一下 YOLOV1,可以看我的上一篇文章:https://blog.csdn.net/dgvv4/article/det ...

  9. 字节跳动在 RocksDB 存储引擎上的改进实践

    本文选自"字节跳动基础架构实践"系列文章. "字节跳动基础架构实践"系列文章是由字节跳动基础架构部门各技术团队及专家倾力打造的技术干货内容,和大家分享团队在基础 ...

最新文章

  1. 乐鑫esp8266模块MicroPython开发板MQTT物联网人工智能最小系统
  2. 3ds max 渲染清晰面片的边缘
  3. 只读副本和Spring Data第2部分:配置基础项目
  4. 线程创建-结束-回收 教程
  5. 天津海运[600751]股票
  6. 华为全面启航计算战略:“鲲鹏+昇腾”双引擎
  7. Mysql学习总结(57)——MySQL查询当天、本周、本月、上周、本周、上月、距离当前现在6个月数据
  8. selenium ruby和java_针对Ruby的Selenium WebDriver安装指南
  9. Java基础:什么是返回对象
  10. Systemd 入门教程:命令篇、实战篇
  11. 第二季-专题9--代码搬移不可少
  12. Caffe学习:使用pycaffe定义网络
  13. LLDP发现相邻设备失败分析
  14. html 数据类型 text,客户端发现响应内容类型为“text/html; charset=utf-8”,但应为“text/xml”。...
  15. 基于单片机的心率监测系统设计(#0495)
  16. 电脑键盘部分按键失灵_笔记本键盘部分失灵怎么办,笔记本个别键失灵的处理方法...
  17. 如何判断如何判断RS232线是直连还是交叉连线
  18. 华为AI四小龙兵临城下,海大宇如何反围剿?
  19. 使用ffmpeg调整图像大小
  20. python 个人收支系统_C/C++实现个人收支系统的示例代码

热门文章

  1. MVC如何添加Model
  2. java 进程运行时间_将Java类作为子进程运行
  3. oidc_使用Java EE和OIDC构建Java REST API
  4. apache kafka_Apache Kafka简介
  5. jdk 细粒度锁_使用JDK 8轻松进行细粒度排序
  6. kafka数据到flume_大数据摄取:Flume,Kafka和NiFi
  7. java 开发 jvm_Java开发人员应了解的JVM流行语
  8. 新版本的Selenium 4 Alpha会有什么期望?
  9. JMetro版本11.6.8和8.6.8发布
  10. spring 异步返回结果_使用Spring Integration聚合异步结果