junit5 入门系列教程-05-junit5 断言(assert)
目录
- 目录
- Assert
- 实例
- 三方 jar
- 系列导航
Assert
JUnit Jupiter附带了许多JUnit 4拥有的断言方法,并添加了一些可以很好地用于Java 8 lambdas的断言方法。
所有JUnit木星断言都是 org.junit.jupiter.api.Assertions
中的静态方法断言类。
实例
- AssertTest.java
import org.junit.jupiter.api.Test;import static java.time.Duration.ofMillis;
import static java.time.Duration.ofMinutes;
import static org.junit.jupiter.api.Assertions.assertAll;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTimeout;
import static org.junit.jupiter.api.Assertions.assertTimeoutPreemptively;
import static org.junit.jupiter.api.Assertions.assertTrue;public class AssertTest {@Testvoid standardAssertions() {assertEquals(2, 2);assertEquals(4, 4, "The optional assertion message is now the last parameter.");assertTrue('a' < 'b', () -> "Assertion messages can be lazily evaluated -- "+ "to avoid constructing complex messages unnecessarily.");}@Testvoid groupedAssertions() {// In a grouped assertion all assertions are executed, and any// failures will be reported together.Person person = new Person().getDefaultPerson();assertAll("person",() -> assertEquals("John", person.getFirstName()),() -> assertEquals("Doe", person.getLastName()));}@Testvoid dependentAssertions() {// Within a code block, if an assertion fails the// subsequent code in the same block will be skipped.Person person = new Person().getDefaultPerson();assertAll("properties",() -> {String firstName = person.getFirstName();assertNotNull(firstName);// Executed only if the previous assertion is valid.assertAll("first name",() -> assertTrue(firstName.startsWith("J")),() -> assertTrue(firstName.endsWith("n")));},() -> {// Grouped assertion, so processed independently// of results of first name assertions.String lastName = person.getLastName();assertNotNull(lastName);// Executed only if the previous assertion is valid.assertAll("last name",() -> assertTrue(lastName.startsWith("D")),() -> assertTrue(lastName.endsWith("e")));});}@Testvoid exceptionTesting() {Throwable exception = assertThrows(IllegalArgumentException.class, () -> {throw new IllegalArgumentException("a message");});assertEquals("a message", exception.getMessage());}@Testvoid timeoutNotExceeded() {// The following assertion succeeds.assertTimeout(ofMinutes(2), () -> {// Perform task that takes less than 2 minutes.});}@Testvoid timeoutNotExceededWithResult() {// The following assertion succeeds, and returns the supplied object.String actualResult = assertTimeout(ofMinutes(2), () -> {return "a result";});assertEquals("a result", actualResult);}@Testvoid timeoutNotExceededWithMethod() {// The following assertion invokes a method reference and returns an object.String actualGreeting = assertTimeout(ofMinutes(2), AssertTest::greeting);assertEquals("Hello, World!", actualGreeting);}@Testvoid timeoutExceeded() {// The following assertion fails with an error message similar to:// execution exceeded timeout of 10 ms by 91 msassertTimeout(ofMillis(10), () -> {// Simulate task that takes more than 10 ms.Thread.sleep(100);});}@Testvoid timeoutExceededWithPreemptiveTermination() {// The following assertion fails with an error message similar to:// execution timed out after 10 msassertTimeoutPreemptively(ofMillis(10), () -> {// Simulate task that takes more than 10 ms.Thread.sleep(100);});}private static String greeting() {return "Hello, World!";}private class Person {private String firstName;private String lastName;public Person() {}public Person(String firstName, String lastName) {this.firstName = firstName;this.lastName = lastName;}public String getFirstName() {return firstName;}public String getLastName() {return lastName;}public Person getDefaultPerson() {return new Person("ryo", "12222");}}
}
三方 jar
尽管JUnit Jupiter提供的断言功能对于许多测试场景来说已经足够了,但是有时需要更多的功能和额外的功能,比如matchers。在这种情况下,JUnit团队推荐使用诸如AssertJ、Hamcrest、Truth等第三方断言库。因此,开发人员可以自由使用他们选择的断言库。
例如,可以使用matchers和fluent API的组合使断言更具描述性和可读性。然而, JUnit Jupiter 的 org.junit.jupiter.api.Assertions
断言类不提供类似于JUnit 4的org.junit中的assertThat()
方法。
接受Hamcrest编码器的断言类。相反,鼓励开发人员使用第三方断言库提供的对匹配器的内置支持。
下面的示例演示如何在JUnit Jupiter测试中使用来自Hamcrest的assertThat()支持。
只要将Hamcrest库添加到类路径中,您就可以静态地导入诸如assertThat()、is()和equalTo()等方法,然后在像assertWithHamcrestMatcher()方法的测试中使用它们。
- HamcrestAssertionDemo.java
当然,基于JUnit 4编程模型的遗留测试可以继续使用org.junit.Assert#assertThat。
如下:
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;import org.junit.jupiter.api.Test;class HamcrestAssertionDemo {@Testvoid assertWithHamcrestMatcher() {assertThat(2 + 1, is(equalTo(3)));}}
系列导航
系列导航
junit5 入门系列教程-05-junit5 断言(assert)相关推荐
- junit5 入门系列教程-02-junit5 注解详解
目录 目录 Junit5 注解 @Test @ParameterizedTest @RepeatedTest @TestFactory @TestInstance @TestTemplate @Dis ...
- junit5 入门系列教程-17-junit5 动态测试(DynamicTest)
目录 目录 动态测试 生命周期 测试案例 系列导航 动态测试 在标注中描述的JUnit Jupiter中的标准@Test注释与JUnit 4中的@Test注释非常相似.两者都描述了实现测试用例的方法. ...
- Provisioning Services 7.8 入门系列教程之十三 使用 Boot Device Management(BDM)
续Provisioning Services 7.8 入门系列教程之十二 实现高可用性 可以使用 Boot Device Management 实用程序将 IP 和引导信息(引导设备)交付给目标设备, ...
- Provisioning Services 7.8 入门系列教程之十四 UEFI支持和BOOTPTAB 编辑器
续Provisioning Services 7.8 入门系列教程之十三 使用 Boot Device Management(BDM) UEFI,全称Unified Extensible Firmw ...
- graphviz 画决策树_数据挖掘入门系列教程(四)之基于scikit-lean决策树处理Iris
数据挖掘入门系列教程(四)之基于scikit-lean决策树处理Iris 加载数据集 数据特征 训练 随机森林 调参工程师 结尾 数据挖掘入门系列教程(四)之基于scikit-lean决策树处理Iri ...
- Provisioning Services 7.8 入门系列教程之十一 通过版本控制自动更新虚拟磁盘
续Provisioning Services 7.8 入门系列教程之十 通过类自动更新虚拟磁盘 从前两的两种更新方式可以看出,它们有一个共同的特点,即需要产生(复制)完成的虚拟磁盘副本,然后进行相关的 ...
- html5游戏制作入门系列教程(八)
今天,我已经准备了一个新的游戏 – SkyWalker.基本上 – 这是用飞飞行模拟射击类游戏.我们的目标到达终点线.这个游戏还有其它一些特点,例如使用飞机运动动画和爆炸动画,多按键处理(例如同时移动 ...
- html5游戏制作入门系列教程(七)
我们继续这一系列文章,使用HTML5的canvas组件进行游戏开发.我们将要更新完善我们的第4课html5游戏制作入门系列教程(四)的游戏实例,并增加了火球,敌人和碰撞检测等功能模块.所以,现在我们的 ...
- html5游戏制作入门系列教程(六)
我们继续这一系列文章,使用HTML5的canvas组件进行游戏开发.今天,我们将创建我们的第一个完整的游戏 – 打砖块.在这一课中,我会告诉你如何检测基本的碰撞和HTML5的本地存储.您可以使用鼠标和 ...
- html5游戏制作入门系列教程(五)
我们继续这一系列文章,使用HTML5的canvas组件进行游戏开发.今天,这是相当完整的游戏例子 – 它会回顾经典的旧电脑游戏 – 坦克大战.我会教你使用阵列地图并教你如何检测活动对象(坦克)与环境( ...
最新文章
- windows xp系统驱动安装问题
- 人工智能时代,中国或是唯一能够和美国竞争的国家!
- android adb install Failure,提示base.apkcode is missing问题的解决
- Java黑皮书课后题第3章:*3.30(当前时间)修改编程练习题2.8,以12小时时钟制显示小时数
- 高中生计算机创新大赛作品,2017 第十届“英特尔杯”全国大学生软件创新大赛获奖作品...
- Objective-C中的@property
- 写一个算法统计在输入字符串中各个字符出现的频度
- Ubuntu下gcc多版本共存和版本切换
- 金融风控必备:想从手机上做风控管理?原来还可以从设备指纹入手
- 《算法图解》——狄克斯特拉算法
- 方法、hadoop源码之JobQueueTaskScheduler-by小雨
- Xshell6 + Xftp6 绿色破解永久授权激活版 免安装 解压即用,最好的SSH工具(Xshell 6 plus套件)
- js 的常用工具类库
- 程序员,你何时离开北京
- 【图解相对论系列1】怎样直观地理解张量(Tensor)?爱因斯坦广义相对论的数学基础...
- 日志打印、main函数中代码执行顺序
- php多用户表白源码,php源码]阿狸表白自动生成源码
- 触碰岁月——土楼潇洒穷游记
- Java 判断实体类对象的全部属性是否空
- 西工大计算机操作系统实验报告,西工大操作系统实验报告os4.doc