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的更多相关文章

  1. Test注解的两个属性(转)

    Test注解的两个属性:expected和timeout Junit的Test注解支持两个可选的参数expected和timeout.expected声明一个测试方法必须抛出一个异常.如果不抛出异常或 ...

  2. Test注解的两个属性:expected和timeout

    JUnit4:Test文档中的解释: The Test annotation supports two optional parameters. The first, expected, declar ...

  3. timestamp的两个属性:CURRENT_TIMESTAMP 和ON UPDATE CURRENT_TIMESTAMP

    timestamp有两个属性,分别是CURRENT_TIMESTAMP 和ON UPDATE CURRENT_TIMESTAMP两种,使用情况分别如下: 1. CURRENT_TIMESTAMP 当要 ...

  4. JSP页面中的pageEncoding和contentType两种属性

    关于JSP页面中的pageEncoding和contentType两种属性的区别: pageEncoding是jsp文件本身的编码 contentType的charset是指服务器发送给客户端时的内容 ...

  5. webservice cxf error:类的两个属性具有相同名称 "password"

    execption detail: Caused by: javax.xml.ws.WebServiceException: org.apache.cxf.service.factory.Servic ...

  6. css pre如果同时运用了css的border-radius、 overflow两个属性且标签中内容太多时,外部div滚动条在firefox下滚动时很卡

    pre如果同时运用了css的border-radius. overflow两个属性且标签中内容太多时,外部div滚动条在firefox下滚动时很卡. 解决方法:去掉css中border-radius. ...

  7. ListBox之类控件的Item项显示对象的两个属性

    wpf项目中,ListBox绑定对象集合,ListBoxItem要显示对象的两个属性,例如:显示员工的工号和姓名. 之前我的做法是在Employee员工类中添加一个"NumAndName&q ...

  8. JavaScript中screen对象的两个属性

    Screen 对象 Screen 对象包含有关客户端显示屏幕的信息. 这里说一下今天用到的两个属性:availHeigth,availWidth avaiHeigth返回显示屏幕的高度 (除 Wind ...

  9. SpringMVC注解@RequestMapping之produces属性导致的406错误

    废话不多说,各位,直接看图说话,敢吗?这个问题网上解决的办法写的狠是粗糙,甚至说这次我干掉它完全是靠巧合,但是也不否认网上针对406错误给出的解决方式,可能是多种情况下出现的406吧?我这次的流程就是 ...

随机推荐

  1. IP地址的分类

    IPv4 地址的分类:   一,组成   1. 使用32位地址 2. 以点分十进制表示,如172.16.0.0,每一个数字对应于8个二进制的比特串,称为一个位组(octets).如某一台主机的IP地址 ...

  2. C之按位运算符

    http://www.cnblogs.com/Kazaf/archive/2012/03/19/2406006.html

  3. C#三级联动

    1.运用ComboBox的控件建立效果,如右图: 2.建立三个表,第一个Province表: PID,PName;第二个为city表: CId,CName,PId;第三个为coun表:CounID,C ...

  4. html学习第一天笔记

    语义化,让你的网页更好的被搜索引擎理解在这一章节我们要开始把网页中常用到的标签一 一向大家介绍,学习这一章节的时候要记住学习html标签过程中,主要注意两个方面的学习:标签的用途.标签在浏览器中的默认 ...

  5. C# 可视化读取文件、文件夹

    OpenFileDialog fd = new OpenFileDialog(); fd.Filter = "txt files (*.txt)|*.txt|All files(*.*)|* ...

  6. Android自动化测试 - Robotium之re-sign.jar重签名后安装失败提示Failure [INSTALL_PARSE_FAILED_NO_CERTIFICATES]解决方案

    问题:在用re-sign.jar重签名apk文件后,显示重签名成功,但在实际安装过程中确提示:Failure [INSTALL_PARSE_FAILED_NO_CERTIFICATES] 原因:网上查 ...

  7. Struts2文件上传与下载

    一,页面 index.html 在页面中最重要的就是这个文件上传用的 form 表单,注意这里一定要把 form 的encyType属性明确标定为“multipart/form-data”,只有这样. ...

  8. Python for Informatics 第11章 正则表达式五(译)

    注:文章原文为Dr. Charles Severance 的 <Python for Informatics>.文中代码用3.4版改写,并在本机测试通过. 11.4 转义字符 之前我们在正 ...

  9. ACM: FZU 2107 Hua Rong Dao - DFS - 暴力

    FZU 2107 Hua Rong Dao Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I6 ...

  10. Django分析之如何自定义manage命令

    我们都用过Django的manage.py的命令,而manage.py是在我们创建Django项目的时候就自动生成在根目录下的一个命令行工具,它可以执行一些简单的命令,其功能是将Django proj ...