Junit的异常测试
方式1:
@Test(expected = IndexOutOfBoundsException.class)
public void empty() {
new ArrayList<Object>().get(0);
}
解析:
@Test的注释有一个可选参数expected,它作为Throwable的子类,可以抛出异常。 方式2:
使用try...catch()..
@Test
public void testExceptionMessage() {
try {
new ArrayList<Object>().get(0);
fail("Expected an IndexOutOfBoundsException to be thrown");
} catch (IndexOutOfBoundsException anIndexOutOfBoundsException) {
assertThat(anIndexOutOfBoundsException.getMessage(), is("Index: 0, Size: 0"));
}
}
方式3:
使用ExpectedException规则,此规则可以指示出异常和异常信息。
@Rule
public ExpectedException thrown = ExpectedException.none(); @Test
public void shouldTestExceptionMessage() throws IndexOutOfBoundsException {
List<Object> list = new ArrayList<Object>(); thrown.expect(IndexOutOfBoundsException.class);
thrown.expectMessage("Index: 0, Size: 0");
list.get(0); // execution will never get past this line
} 方式4:
使用Matchers检查异常
import static org.hamcrest.Matchers.hasProperty;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.startsWith; import javax.ws.rs.NotFoundException;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.Status; import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException; public class TestExy {
@Rule
public ExpectedException thrown = ExpectedException.none(); @Test
public void shouldThrow() {
TestThing testThing = new TestThing();
thrown.expect(NotFoundException.class);
thrown.expectMessage(startsWith("some Message"));
thrown.expect(hasProperty("response", hasProperty("status", is(404))));
testThing.chuck();
} private class TestThing {
public void chuck() {
Response response = Response.status(Status.NOT_FOUND).entity("Resource not found").build();
throw new NotFoundException("some Message", response);
}
}
}
更多信息点击此链接:
https://github.com/junit-team/junit4/wiki/Exception-testing
Junit的异常测试的更多相关文章
- Java高级特性 第11节 JUnit 3.x和JUnit 4.x测试框架
一.软件测试 1.软件测试的概念及分类 软件测试是使用人工或者自动手段来运行或测试某个系统的过程,其目的在于检验它是否满足规定的需求或弄清预期结果与实际结果之间的差别.它是帮助识别开发完成(中间或最终 ...
- 廖雪峰Java8JUnit单元测试-2使用JUnit-2异常测试
1.异常测试 对可能抛出的异常进行测试: 异常本身是方法签名的一部分: * public static int parseInt(String s) throws NumberFormatExcept ...
- Spring-test使用JUnit时,测试类autowired报错,create bean error
Spring-test使用JUnit时,测试类里面使用autowired会报错, 报create bean error...... 但是controller里面@autowired可以正常运行的. 在 ...
- Android中使用自身携带的Junit新建一个测试工程
1.新建立一个Android工程 package com.shellway.junit; public class Service { public int divide(int a,int b){ ...
- [深入JUnit] 为什么别测试private函数
[深入JUnit] 为什么别测试private函数 摘自http://www.tuicool.com/articles/iumaayJ 时间 2016-03-28 10:58:03 SegmentFa ...
- TestNg 6.异常测试
* 什么时候会用到异常测试??* 在我们期望结果为某一个异常的时候* 比如:我们传入了某些不合法的参数,程序抛出异常* 也就是我的预期结果就是这个异常看以下的一段代码: package com.cou ...
- testng入门教程8 TestNG异常测试
TestNG跟踪异常处理代码提供了一个选项.可以测试是否需要代码抛出异常或不抛出. @Test注释expectedExceptions 参数一起使用.现在,让我们来看看@Test(expectedEx ...
- Maven聚合、Maven仓库jar包以及Spring+MyBatis+JUnit+Maven整合测试的搭建过程
一.Maven将父项目创建到父项目的内部 在父项目的pom.xml上 点右键,选择maven-->new-->maven module project 二.Maven聚合 在某个项目的p ...
- TestNG异常测试
用@Test(expectedExceptions = xxx) 声明 package com.janson; import org.testng.annotations.Test; public c ...
随机推荐
- python 将文件大小转换为human readable 的大小表示
定义了一个函数, def HRS(size): units=('B','KB','MB','GB','TB','PB') for i in range(len(units)-1,-1,-1 ...
- 谷歌Cookies无法写入
写Cookies页面加这个ok: Response.AddHeader("P3P", "CP=CAO PSA OUR");
- sitecore系统教程之体验编辑器中创建一个项目
您可以使用体验编辑器创建新项目并将其直接插入网页. 注意 如何在Sitecore中创建项目可能会有所不同,具体取决于您拥有的安全角色以及网站的设置方式. 要插入新项目: 在体验编辑器中,导航到要添加新 ...
- Math对象属性
2018-11-28 11:18:46
- Spark学习之路 (十四)SparkCore的调优之资源调优JVM的GC垃圾收集器
一.概述 垃圾收集 Garbage Collection 通常被称为“GC”,它诞生于1960年 MIT 的 Lisp 语言,经过半个多世纪,目前已经十分成熟了. jvm 中,程序计数器.虚拟机栈.本 ...
- 不能安装vmtools解决:一个命令安装
https://blog.csdn.net/fly66611/article/details/77994339 换好源 sudo su apt-get update apt-get dist-upgr ...
- flask 表单
表单 在Web程序中,表单时和用户交互最常见的方式之一.用户注册.登录.撰写文章.编辑设置,无一不用到表单.不过,表单的处理不简单.要创建表单,验证用户输入的内容,向用户显示错误提示,还要获取并保存数 ...
- docker能用来干嘛
http://blog.csdn.net/wangtaoking1/article/details/44340445 什么是Docker Docker 是一个开源项目,诞生于 2013 年初,最初 ...
- linux下VLAN设置
1. 安装vlan(vconfig)和加载8021q模块 [root@test0001~]#yum install vconfig [root@test0001~]#modprobe 8021q [r ...
- iOS项目之解析HTML数据
最近因为需求,一直在做HTML数据的解析,从网页中去获取需要的数据,然后展示到自己的app中. 在网上找了很多资料,大多都是TFHpple这个第三方框架,能够根据标签节点获取对应的数据,但是现在我需要 ...