Junit - 超时测试(Timeout Test)
发布日期:2021-06-30 23:46:27
浏览次数:2
分类:技术文章
本文共 1501 字,大约阅读时间需要 5 分钟。
Junit 4超时测试(Timeout test)可以被用来测试方法的执行时间。 Junit 4 超时测试可以被用在:
- 在测试类的方法上使用 @Timeout 注解
- 测试类的所有方法应用 Timeout 规则
一、在测试类的方法上使用 @Timeout 注解
Junit 4 提供了 @Timeout 注解来测试任意特定方法的执行时间。如果测试方法的执行时间大于指定的超时参数,测试方法将抛出异常,测试结果为失败。指定的超时参数是以毫秒记。
@Timeout 注解样例
TimeoutTest.java test class for timeout test
package in.co.javatutorials; import org.junit.Test; /** * @author javatutorials.co.in */public class TimeoutTest { /** * Example of timeout test. * Test will fail if it takes more than 200 ms to execute */ @Test(timeout = 200) public void testTimeout() { while (true); }}
样例输出
结果在 eclipse junit 窗口中显示如下:
二、测试类的所有方法应用 Timeout 规则
Junit 4 提供了 Timeout 规则来测试类中的所有方法。如果类中的任意一个方法执行时间超过了在Timeout 规则中规定的值,测试方法将抛出异常,测试结果为失败。指定的超时参数是以毫秒记。
Timeout 规则
TimeoutRuleTest.java 测试Timeout 规则的测试类:
package in.co.javatutorials; import org.junit.Rule;import org.junit.Test;import org.junit.rules.Timeout; /** * @author javatutorials.co.in */public class TimeoutRuleTest { /** * Rule is applied to all methods of class * time is specified in milliseconds */ @Rule public Timeout timeout = new Timeout(1000); /** * Example of timeout test. * Test will fail if it takes more than 1 sec to execute */ @Test public void testTimeout1() { while(true); } /** * Example of timeout test. * Test will fail if it takes more than 1 sec to execute */ @Test public void testTimeout2() { while(true); } }
样例输出
结果在 eclipse junit 窗口中显示如下:
转载地址:https://lux-sun.blog.csdn.net/article/details/83860356 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!
发表评论
最新留言
第一次来,支持一个
[***.219.124.196]2024年04月21日 14时20分40秒
关于作者
喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
loadbalancer动态刷新nacos的server
2019-05-01
@FeignClient注解的重复名称解决
2019-05-01
org.openjdk.jol
2019-05-01
自己解析class文件
2019-05-01
access_flags
2019-05-01
ClassFile之Fields
2019-05-01
ClassFile之Methods
2019-05-01
ClassFile之Attributes
2019-05-01
ClassFile - Attributes - Code
2019-05-01
jvm虚拟机指令
2019-05-01
JMeter报告属性
2019-05-01
mysql高级语句
2019-05-01
mysql函数
2019-05-01
scala var val
2019-05-01
oracle sql
2019-05-01
spark单机安装
2019-05-01
java.net.BindException: 无法指定被请求的地址
2019-05-01
scala list
2019-05-01
多人协作流程与规范
2019-05-01
k8s设置阿里云仓库
2019-05-01