JUnit4: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:Test注解的两个属性:expected和timeout的更多相关文章
- Test注解的两个属性(转)
Test注解的两个属性:expected和timeout Junit的Test注解支持两个可选的参数expected和timeout.expected声明一个测试方法必须抛出一个异常.如果不抛出异常或 ...
- 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吧?我这次的流程就是 ...
随机推荐
- 【Java EE 学习 44】【Hibernate学习第一天】【Hibernate对单表的CRUD操作】
一.Hibernate简介 1.hibernate是对jdbc的二次开发 2.jdbc没有缓存机制,但是hibernate有. 3.hibernate的有点和缺点 (1)优点:有缓存,而且是二级缓存: ...
- ASP.NET4.5Web API及非同步程序开发系列(1)
认识非同步程序开发设计模型 从VS2012开始引入的新的非同步程序设计的支持-------async/await设计模型 之前的当我们支持非同步作业的时候,往往使用多线程开解决,我们比较熟悉的就是 执 ...
- 特征检测之Haar
Harr特征, 主要用于人脸检测,可以参考我的博文 基于MATLAB的adaboost级联形式的人脸检测实现 1 harr特征的原理 2 haar特征的计算 3 haar特征的应用
- linux 自编软件运行时权限不足问题
在非根目录下" ./*.out " 文件时出现显示运行的权限不足的问题,经过调查,有人给出方案: 方案1: 在Linux下执行一个文件时候提示“权限不够”的解决办法如下 转到那个文 ...
- Javascript中的apply与call详解
JavaScript中有一个call和apply方法,其作用基本相同,但也有略微的区别. 一.方法定义 1.call 方法 语法:call([thisObj[,arg1[, arg2[, [,.arg ...
- myBaties 和 mysql开发中遇到的问题
最近开发内部平台遇到mysql 中的一个问题,order by语句需要在limit 之后. myBaties在parameterType="java.lang.String" 不能 ...
- PHP 删除目录及目录下文件
<?php function del_dir ($dir,$type=true){ $n=0; if (is_dir($dir)) { if ($dh = opendi ...
- CodeForces 279D The Minimum Number of Variables 题解
题目大意: 有一组n个不相同的数字组成数串:a1,a2,a3-an. 1.一个数组b. 2.第一个操作我们将b0的值赋为a1.之后我们有n-1个操作,第k次操作我们将by=bi+bj(y,i,j可能相 ...
- JAVA面试题1
1.在main(String[] args)方法内是否可以调用一个非静态方法? 答案:不能[public static void main(String[] args){}] 2.同一个文件里是否可以 ...
- 移动端/H5关于cursor:pointer导致的问题
cursor属性规定要显示的光标的类型(形状),该属性定义了鼠标指针放在一个元素边界范围内时所用的光标形状(不过 CSS2.1 没有定义由哪个边界确定这个范围). 不过,这个属性用在PC端没有任何问题 ...