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 ...
 
随机推荐
- ActiveMQ漏洞利用方法总结
			
转载来自:http://www.freebuf.com/column/161188.html 1.Console存在默认端口和默认密码/未授权访问(默认密码为admin:admin) ActiveMQ ...
 - WebSocket.之.基础入门-断开连接处理
			
ebSocket.之.基础入门-断开连接处理 在<WebSocket.之.基础入门-后端响应消息>的代码基础之上,继续更新代码.代码只改动了:TestSocket.java 和 index ...
 - vs使用gitflow
			
1.背景:之前在开发一个项目时,用tfs管理代码,并用“禁止多人编辑”来避免冲突,但仅适用于开发团队较小时.缺点: (1).开发团队较大,开发人员较多时,会出现经常互相锁,增加沟通成本.比如增加文件时 ...
 - JavaScript三种判断语句和三元运算符
			
三种判断语句 1.if结构 语法:if(条件){条件满足时执行的代码块} 2.if else结构 语法:if(条件){条件满足时执行的代码块} else{条件不满足时执行的代码块} 3.if el ...
 - D Tree Requests dfs+二分 D Pig and Palindromes -dp
			
D time limit per test 2 seconds memory limit per test 256 megabytes input standard input output stan ...
 - PGPDesktop在win7环境下的安装和使用
			
PGPDesktop在win7环境下的安装和使用 PGP的简介 PGP(Pretty Good Privacy),是一个基于RSA公钥加密体系的邮件加密软件,它提供了非对称加密和数字签名,是目前非常流 ...
 - npm 代理的设置和取消
			
管理员权限下的控制台输入: 设置代理 npm config set proxy=http://127.0.0.1:8080 npm config set registry=http://registr ...
 - Spring整合Shiro
			
apache shiro 是一个安全认证框架,和 spring security 相比,在于他使用了比较简洁易懂的 认证和授权方式.其提供的 native-session(即把用户认证后的授权信息保存 ...
 - 第三方统计分析埋点工具对比,神策、Ptmind、GrowingIO、国双,还有谷歌分析,谁更好?
			
第三方统计分析埋点工具对比,神策.Ptmind.GrowingIO.国双,还有谷歌分析,谁更好?https://www.colabug.com/2985393.html GA.Mixpanel 和神策 ...
 - winscp 怎么用私钥文件登录的,以.ppk结尾的密钥文件
			
Winscp默认用帐号密码登录,用私钥文件登录需要在高级选项里面的SSH--验证里面选择文件. Winscp使用的是putty作为SSH登录工具,而puttygen所生成的是以.ppk结尾的密钥文件. ...