怎样去验证代码是否抛出我们期望的异常呢?虽然在代码正常结束时候验证很重要,但是在异常的情况下确保代码如我们希望的运行也很重要。比如说:

new ArrayList<Object>().get(0);

这句代码会抛出一个IndexOutOfBoundsException异常。有三种方法来验证ArrayList是否抛出了正确的异常。

1. @Test 里面加上一个参数"expected"。(参见test01)

谨慎使用expected参数。方法里的任意代码抛出IndexOutOfBoundsException异常都会导致该测试pass。复杂的测试建议使用 ExpectedException 规则。

2.Try/Catch(参见test02)

方法1仅仅适用于简单的例子,它有它的局限性。比如说,我们不能测试异常的信息返回值,或者异常抛出之后某个域对象的状态。对于这种需求我们可以采用JUnit 3.x流行的 try/catch 来实现。

3.ExpectedException 规则(参见test03)

该规则不仅可以测试抛出的异常,还可以测试期望的异常信息。

import static org.junit.Assert.*;
import static org.hamcrest.CoreMatchers.*;
import java.util.ArrayList;
import java.util.List;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException; public class ExceptionTest { @Test(expected=IndexOutOfBoundsException.class)
public void test01(){
new ArrayList<Object>().get(0);
} @Test()
public void test02(){
try{
new ArrayList<Object>().get(0);
fail("Expected an IndexOutOfBoundsException to be thrown");
}catch(IndexOutOfBoundsException e){
assertThat(e.getMessage(),is("Index: 0, Size: 0"));
}
} @Rule
public ExpectedException thrown=ExpectedException.none(); @Test
public void test03() throws IndexOutOfBoundsException{
List<Object> list=new ArrayList<Object>();
thrown.expect(IndexOutOfBoundsException.class);
thrown.expectMessage("Index: 0, Size: 0");
list.get(0);
}
}

Exception testing的更多相关文章

  1. Rules

    我们之前处理异常的时候用到过Rules,当然还有很多其他规则.Rules允许弹性的添加或者重定义测试方法的行为.测试者可以重复使用或者扩展下面的某一个Rules,也可以写一个属于自己的规则. 这里先展 ...

  2. JUnit4单元测试基础篇

    引言 JUnit作为Java语言的测试框架,在测试驱动开发(TDD)下扮演重要的角色.众所周知,无论开发大型项目还是一般的小型项目, 单元测试都至关重要.单元测试为软件可发测试维护提供了很大的便利.J ...

  3. huffman树实现的压缩算法,java

    1.树的构建 package huffman; public abstract class BinaryTreeBasis { protected TreeNode root; public Bina ...

  4. spring mvc: log4j插件 log日志的输出

    准备: log插件:log4j <!-- log日志插件 --> <!-- https://mvnrepository.com/artifact/log4j/log4j --> ...

  5. Spring MVC集成Log4j

    以下示例显示如何使用Spring Web MVC框架集成LOG4J.首先使用Eclipse IDE,并按照以下步骤使用Spring Web Framework开发基于动态表单的Web应用程序: 创建一 ...

  6. sanic官方文档解析之Example(二)

    1,通过docker部署sanic项目 通过Docker和Docker Compose部署SANIC应用程序是一项很容易实现的任务,下面的示例提供了示例simple_server.py的部署 FROM ...

  7. Spring MVC-集成(Integration)-集成LOG4J示例(转载实践)

    以下内容翻译自:https://www.tutorialspoint.com/springmvc/springmvc_log4j.htm 说明:示例基于Spring MVC 4.1.6. 以下示例说明 ...

  8. Java-Class-@I:org.junit.Test

    ylbtech-Java-Class-@I:org.junit.Test 1.返回顶部   2.返回顶部   3.返回顶部   4.返回顶部 1. package org.junit; import ...

  9. 断言(Assert)与异常(Exception)

    断言是被用来检查非法情况而不是错误情况,即在该程序正常工作时绝不应该发生的非法情况,用来帮助开发人员对问题的快速定位.异常处理用于对程序发生异常情况的处理,增强程序的健壮性.容错性,减少程序使用中对用 ...

随机推荐

  1. jquery在ie浏览器下中文乱码的问题

    用jquery的ajax方法在调用后台数据发现中文乱码,无法解析中文的url,而在别的浏览器下面就不会,如下所示 $.ajax({ type:'get', url:'薛之谦-演员.lrc', asyn ...

  2. AndroidStudio SDK版本下载

    错误描述: pkg: /data/local/tmp/com.example.myapplication Failure [INSTALL_FAILED_OLDER_SDK] 出现这个错误,研究了半天 ...

  3. JavaScript typeof, null, 和 undefined

    typeof 操作符 你可以使用 typeof 操作符来检测变量的数据类型. 实例 typeof "John"                // 返回 string typeof ...

  4. Android入门随记

    1.Activity是通过startActivity()开始的,结束后不反回任何结果,而用startActivityForResult(Intent intent, int resquestCode) ...

  5. C++拾遗(三)关于复合类型

    数组相关 初始化只能在定义的时候使用,不能把数组赋给另一个数组. 初始化可以提供比元素数目少的初值,其它元素将被置为0. 字符char数组只有在以\0结尾时才是一个字符串.sizeof()返回数组的长 ...

  6. ucenter 通信原理

    1.用户登录discuz,通过logging.php文件中的函数uc_user_login对post过来的数据进行验证,也就是对username和password进行验证. 2.如果验证成功,将调用位 ...

  7. C语言对数组取地址

    #include <stdio.h> main() { ] = {,,,,}; printf("a=%p\n" , a); printf("a=%p\n&qu ...

  8. easyui combo+pagination 图标选择器

    从数据库读取分页显示 $(function () { initTable(1, 180); $('#cc').combo({ editable: false, panelWidth: 'auto', ...

  9. DNS解析原理

    1.在浏览器中输入www.qq.com域名,操作系统会先检查自己本地的hosts文件是否有这个网址映射关系,如果有,就先调用这个IP地址映射,完成域名解析. 2.如果hosts里没有这个域名的映射,则 ...

  10. java 属性

    //非静态类 不能定义静态属性/方法/静态类, 可以定义静态常量属性. public class A{ public class B{ public static String  _str; //❌, ...