JUnit5 技术前瞻
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.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; import org.junit.jupiter.api.Test; class AssertionsDemo { @Test
void standardAssertions() {
assertEquals(2, 2);
assertEquals(4, 4, "The optional assertion message is now the last parameter.");
assertTrue(2 == 2, () -> "Assertion messages can be lazily evaluated -- "
+ "to avoid constructing complex messages unnecessarily.");
} @Test
void groupedAssertions() {
//分组断言:在一个分组断言中,所有断言都会被执行,并且任何失败的断言也会一起被报告
assertAll("address",
() -> assertEquals("John", address.getFirstName()),
() -> assertEquals("User", address.getLastName())
);
} @Test
void exceptionTesting() {
Throwable exception = assertThrows(IllegalArgumentException.class, () -> {
throw new IllegalArgumentException("a message");
});
assertEquals("a message", exception.getMessage());
}
@Test
void timeoutNotExceeded() {
// 下面的断言成功
assertTimeout(ofMinutes(2), () -> {
// 执行任务只需不到2分钟
});
} @Test
void timeoutNotExceededWithResult() {
// 以下断言成功,并返回所提供的对象
String actualResult = assertTimeout(ofMinutes(2), () -> {
return "a result";
});
assertEquals("a result", actualResult);
} @Test
void timeoutNotExceededWithMethod() {
// 以下断言调用方法参照并返回一个对象
String actualGreeting = assertTimeout(ofMinutes(2), AssertionsDemo::greeting);
assertEquals("hello world!", actualGreeting);
}
@Test
void timeoutExceeded() {
// 下面的断言失败,类似的错误信息:
// execution exceeded timeout of 10 ms by 91 ms
assertTimeout(ofMillis(10), () -> {
// 模拟的任务,需要超过10毫秒
Thread.sleep(100);
});
} @Test
void timeoutExceededWithPreemptiveTermination() {
// 下面的断言失败,类似的错误信息:
// execution timed out after 10 ms
assertTimeoutPreemptively(ofMillis(10), () -> {
// 模拟的任务,需要超过10毫秒
Thread.sleep(100);
});
} private static String greeting() {
return "hello world!";
} }
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 { @Test
void assertWithHamcrestMatcher() {
assertThat(2 + 1, is(equalTo(3)));
} }
附录:参考
JUnit5 技术前瞻的更多相关文章
- 单元测试系列之六:JUnit5 技术前瞻
更多原创测试技术文章同步更新到微信公众号 :三国测,敬请扫码关注个人的微信号,感谢! 原文链接:http://www.cnblogs.com/zishi/p/6868495.html JUnit ...
- 单元测试系列:Mock工具之Mockito实战
更多原创测试技术文章同步更新到微信公众号 :三国测,敬请扫码关注个人的微信号,感谢! 原文链接:http://www.cnblogs.com/zishi/p/6780719.html 在实际项目中写单 ...
- 单元测试系列:Mock工具Jmockit使用介绍
更多原创测试技术文章同步更新到微信公众号 :三国测,敬请扫码关注个人的微信号,感谢! 原文链接:http://www.cnblogs.com/zishi/p/6760272.html Mock工具Jm ...
- 单元测试系列之五:Mock工具之Mockito实战
更多原创测试技术文章同步更新到微信公众号 :三国测,敬请扫码关注个人的微信号,感谢! 原文链接:http://www.cnblogs.com/zishi/p/6780719.html 在实际项目中写单 ...
- 单元测试系列之十一:Jmockit之mock特性详解
本文是Jmockit学习过程中,根据官网所列的工具特性进行解读. 1.调用次数约束(Invocation count constraints) 可以通过调用计数约束来指定预期和/或允许匹配给定期望的调 ...
- 单元测试系列之十:Sonar 常用代码规则整理(二)
摘要:帮助公司部署了一套sonar平台,经过一段时间运行,发现有一些问题出现频率很高,因此有必要将这些问题进行整理总结和分析,避免再次出现类似问题. 作者原创技术文章,转载请注明出处 ======== ...
- 单元测试系列之九:Sonar 常用代码规则整理(一)
更多原创测试技术文章同步更新到微信公众号 :三国测,敬请扫码关注个人的微信号,感谢! 摘要:公司部署了一套sonar,经过一段时间运行,发现有一些问题出现频率很高,因此有必要将这些问题进行整理总结和分 ...
- 单元测试系列之八:Sonar 数据库表关系整理一(续)
更多原创测试技术文章同步更新到微信公众号 :三国测,敬请扫码关注个人的微信号,感谢! 简介:Sonar平台是目前较为流行的静态代码扫描平台,为了便于使用以及自己二次开发,有必要对它的数据库结构进行学习 ...
- 单元测试系列之一:如何使用JUnit、JaCoCo和EclEmma提高单元测试覆盖率
更多原创测试技术文章同步更新到微信公众号 :三国测,敬请扫码关注个人的微信号,感谢! 原文链接:http://www.cnblogs.com/zishi/p/6726664.html -----如 ...
随机推荐
- python端口扫描器
吃了个火鸡面后感觉到了怀疑人生!!!!!!!!!妈耶,在也不吃了.思路都给辣没了!!! python端口扫描器代码如下: #-*-coding:utf-8 from socket import * i ...
- 2017 Multi-University Training Contest - Team 9 1001&&HDU 6161 Big binary tree【树形dp+hash】
Big binary tree Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)T ...
- codeforces 746C 模拟
C. Tram time limit per test 1 second memory limit per test 256 megabytes input standard input output ...
- CentOS 6.5 编译安装 LNMP环境
建立一个软件包目录存放 mkdir -p /usr/local/src/ 清理已经安装包 rpm -e httpd rpm -e mysql rpm -e php yum -y remove http ...
- Spring最核心的功能是什么?使用Spring框架的最核心的原因是什么?
quote:Spring最核心的功能是什么?使用Spring框架的最核心的原因是什么? (IT公司面试手册,可以多看看) spring 框架中核心组件有三个:Core.Context 和 Beans. ...
- [国嵌攻略][077][Linux时间编程]
时间类型 Coordinated Universal Time(UTC):世界标准时间,也就是格林威治时间(Greenwich Mean Time, GMT). Calendar Time:日历时间, ...
- 自己编写JavaScript的sort函数
在平常开发中我们经常会遇到对数组进行排序的场景,js给我们提供了sort方法可以对数组元素进行排序,默认是按ASCII字母表顺序排序,请看下面例子: var a = [1, 3, 2, 4];var ...
- ubuntu 下 apt /apt-get command not found 命令找不到
简介:apt 命令在ubuntu下找不到.(针对云平台,等可联网的ubuntu 如果是虚拟机,请确认能否联网 (如是虚拟机且不能联网请参考其他文章,大致方向是先挂载系统镜像再安装)) (ps:一般的 ...
- bat脚本设置系统环境变量即时生效
关于bat的资料多但零碎,记录一下. 1.设置环境变量即时生效:通过重启explorer来实现即时生效(亲测有效) @echo off set curPath=%cd% wmic ENVIRONMEN ...
- 与改写url取文件的方法:NetworkRequest和DataAccessSerivice 文件
与改写url取文件的方法:NetworkRequest和DataAccessSerivice 文件 CMDNMapDataCache.cpp 读取二进制代码的方法
