Test注解的两个属性:expected和timeout
JUnit4:Test文档中的解释:
The Test annotation supports two optional parameters.
The first, expected, declares that a test method should throw an exception.
If it doesn't throw an exception or if it throws a different exception than the one declared, the test fails.
For example, the following test succeeds:
@Test(expected=IndexOutOfBoundsException.class)
public void outOfBounds()
{
new ArrayList<Object>().get(1);
}
The second optional parameter, timeout, causes a test to fail if it takes longer than a specified amount of clock time (measured in milliseconds).
The following test fails:
@Test(timeout=100)
public void infinity()
{
while(true);
}
文档中说得比较清楚,下面再结合之前加减乘除的例子重复地解释一下,以作巩固。。
expected属性
用来指示期望抛出的异常类型。
比如除以0的测试:
@Test(expected = Exception.class)
public void testDivide() throws Exception
{
cal.divide(1, 0);
}
抛出指定的异常类型,则测试通过 。
如果除数改为非0值,则不会抛出异常,测试失败,报Failures。
timeout属性
用来指示时间上限。
比如把这个属性设置为100毫秒:
@Test(timeout = 100)
当测试方法的时间超过这个时间值时测试就会失败。
(注意超时了报的是Errors,如果是值错了是Failures)。
附上程序例子:
其中让加法的时间延迟500毫秒。
Calculator package com.mengdd.junit4; public class Calculator
{
public int add(int a, int b)
{
try
{
Thread.sleep(500);
}
catch (InterruptedException e)
{
e.printStackTrace();
} return a + b;
} public int subtract(int a, int b)
{
return a - b;
} public int multiply(int a, int b)
{
return a * b;
} public int divide(int a, int b) throws Exception
{
if (0 == b)
{
throw new Exception("除数不能为0");
}
return a / b;
}
}
测试类代码:
加法方法测试加入了时间限制,导致超过时间时发生错误。
加入了除法除以零的抛出异常测试。
CalculatorTest package com.mengdd.junit4; import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test; import static org.junit.Assert.assertEquals;//静态导入 public class CalculatorTest
{
private Calculator cal = null; @BeforeClass
public static void globalInit()
{
System.out.println("global Init invoked!");
} @AfterClass
public static void globalDestroy()
{
System.out.println("global Destroy invoked!");
} @Before
public void init()// setUp()
{ cal = new Calculator();
System.out.println("init --> cal: " + cal); } @After
public void destroy()// tearDown()
{
System.out.println("destroy");
} @Test(timeout = 100)
public void testAdd()
{
System.out.println("testAdd");
int result = cal.add(3, 5);
assertEquals(8, result);
} @Test
public void testSubtract()
{
System.out.println("testSubtract");
int result = cal.subtract(1, 6);
assertEquals(-5, result); } @Test
public void testMultiply()
{
System.out.println("testMultiply");
int result = cal.multiply(5, 9);
assertEquals(45, result);
} @Test(expected = Exception.class)
public void testDivide() throws Exception
{
cal.divide(1, 0);
} }
参考资料
圣思园张龙老师视频教程。
JUnit4 chm格式文档网盘下载链接:
JUnit 4.0:http://pan.baidu.com/share/link?shareid=539345&uk=2701745266
Test注解的两个属性:expected和timeout的更多相关文章
- Test注解的两个属性(转)
Test注解的两个属性:expected和timeout Junit的Test注解支持两个可选的参数expected和timeout.expected声明一个测试方法必须抛出一个异常.如果不抛出异常或 ...
- JUnit4:Test注解的两个属性:expected和timeout
JUnit4:Test文档中的解释: The Test annotation supports two optional parameters. The first, expected, declar ...
- timestamp的两个属性:CURRENT_TIMESTAMP 和ON UPDATE CURRENT_TIMESTAMP
timestamp有两个属性,分别是CURRENT_TIMESTAMP 和ON UPDATE CURRENT_TIMESTAMP两种,使用情况分别如下: 1. CURRENT_TIMESTAMP 当要 ...
- JSP页面中的pageEncoding和contentType两种属性
关于JSP页面中的pageEncoding和contentType两种属性的区别: pageEncoding是jsp文件本身的编码 contentType的charset是指服务器发送给客户端时的内容 ...
- webservice cxf error:类的两个属性具有相同名称 "password"
execption detail: Caused by: javax.xml.ws.WebServiceException: org.apache.cxf.service.factory.Servic ...
- css pre如果同时运用了css的border-radius、 overflow两个属性且标签中内容太多时,外部div滚动条在firefox下滚动时很卡
pre如果同时运用了css的border-radius. overflow两个属性且标签中内容太多时,外部div滚动条在firefox下滚动时很卡. 解决方法:去掉css中border-radius. ...
- ListBox之类控件的Item项显示对象的两个属性
wpf项目中,ListBox绑定对象集合,ListBoxItem要显示对象的两个属性,例如:显示员工的工号和姓名. 之前我的做法是在Employee员工类中添加一个"NumAndName&q ...
- JavaScript中screen对象的两个属性
Screen 对象 Screen 对象包含有关客户端显示屏幕的信息. 这里说一下今天用到的两个属性:availHeigth,availWidth avaiHeigth返回显示屏幕的高度 (除 Wind ...
- SpringMVC注解@RequestMapping之produces属性导致的406错误
废话不多说,各位,直接看图说话,敢吗?这个问题网上解决的办法写的狠是粗糙,甚至说这次我干掉它完全是靠巧合,但是也不否认网上针对406错误给出的解决方式,可能是多种情况下出现的406吧?我这次的流程就是 ...
随机推荐
- 辛星解读mysql的用户管理
可能做开发的多半不太关注这方面,可是要说到做运维.那就不能不关注了.由于我们都知道,root的权限太大了.不是随便能用的.我们平时最好用一些比較低的权限的用户.这样会让我们的安全性大大提高,也能防止我 ...
- Network 20Q--Q2 How does Google sell ad spaces?
在使用Google搜索的时候会发现,搜索出来的页面除了在左边显示搜索结果以外,还会页面的右边推荐一些广告.那么Google是怎么从这些广告挣钱以及广告商可以通过Google广告获得什么利益呢? Goo ...
- Gson源码分析之Json结构抽象和注解使用
github上的博客地址: http://chuyun923.github.io/blog/2015/01/06/gsonyuan-ma-fen-xi/ XML和Json作为最常用的两种网络传输格式而 ...
- Hacker(22)----解除系统中的密码
Win7系统中,用户可以设置BIOS密码以及系统登录密码,这些密码都有各自的用途.但对黑客而言,完全可以采用不同的方式绕过这些密码.下面介绍黑客如如何接触这些密码. 一.解除BIOS密码 BIOS密码 ...
- vsftp虚拟用户登录配置详解
一.安装:1.安装Vsftpd服务:# yum install vsftpd 2.安装DB4部件包:这里要特别安装一个db4的包,用来支持文件数据库.# yum install db4-utils 二 ...
- getAttribute()与getParameter的区别
当两个Web组件之间为转发关系时,转发源会将要共享 request范围内的数据先用setAttribute将数据放入到HttpServletRequest对象中,然后转发目标通过 getParamet ...
- 微软HoloLens虚拟现实可以开发了。
1.microsoft-hololens-now-available-to-developers 2.http://www.microsoft.com/microsoft-hololens/en-us ...
- C#中的表达式树的浅解
表达式树可以说是Linq的核心之一,为什么是Linq的核心之一呢?因为表达式树使得c#不再是仅仅能编译成IL,我们可以通过c#生成一个表达式树,将结果作为一个中间格式,在将其转换成目标平台上的本机语言 ...
- Struts2.3.16.3 基本9个jar包
实践证明,Struts2.3.16.3 至少要下面9个Jar包才能正常启动. commons-fileupload-1.3.1.jar commons-logging-1.1.3.jar freema ...
- memcached真实项目中的应用
上一篇memcached介绍及基本使用介绍了memcached的一些基本概念和一个范例.这一篇将介绍一个memcached在实际项目中的应用