方式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的异常测试的更多相关文章

  1. Java高级特性 第11节 JUnit 3.x和JUnit 4.x测试框架

    一.软件测试 1.软件测试的概念及分类 软件测试是使用人工或者自动手段来运行或测试某个系统的过程,其目的在于检验它是否满足规定的需求或弄清预期结果与实际结果之间的差别.它是帮助识别开发完成(中间或最终 ...

  2. 廖雪峰Java8JUnit单元测试-2使用JUnit-2异常测试

    1.异常测试 对可能抛出的异常进行测试: 异常本身是方法签名的一部分: * public static int parseInt(String s) throws NumberFormatExcept ...

  3. Spring-test使用JUnit时,测试类autowired报错,create bean error

    Spring-test使用JUnit时,测试类里面使用autowired会报错, 报create bean error...... 但是controller里面@autowired可以正常运行的. 在 ...

  4. Android中使用自身携带的Junit新建一个测试工程

    1.新建立一个Android工程 package com.shellway.junit; public class Service { public int divide(int a,int b){ ...

  5. [深入JUnit] 为什么别测试private函数

    [深入JUnit] 为什么别测试private函数 摘自http://www.tuicool.com/articles/iumaayJ 时间 2016-03-28 10:58:03 SegmentFa ...

  6. TestNg 6.异常测试

    * 什么时候会用到异常测试??* 在我们期望结果为某一个异常的时候* 比如:我们传入了某些不合法的参数,程序抛出异常* 也就是我的预期结果就是这个异常看以下的一段代码: package com.cou ...

  7. testng入门教程8 TestNG异常测试

    TestNG跟踪异常处理代码提供了一个选项.可以测试是否需要代码抛出异常或不抛出. @Test注释expectedExceptions 参数一起使用.现在,让我们来看看@Test(expectedEx ...

  8. Maven聚合、Maven仓库jar包以及Spring+MyBatis+JUnit+Maven整合测试的搭建过程

    一.Maven将父项目创建到父项目的内部 在父项目的pom.xml上 点右键,选择maven-->new-->maven module  project 二.Maven聚合 在某个项目的p ...

  9. TestNG异常测试

    用@Test(expectedExceptions = xxx) 声明 package com.janson; import org.testng.annotations.Test; public c ...

随机推荐

  1. ES6之箭头表达式

    ES2015新增的特性箭头表达式,省去了关键词function,文中``双撇号和${}是Typescript的语法.以下是一些学习笔记: 1.单行箭头表达式: var foo = (arg1,arg2 ...

  2. Lambda表达式语法2

    package airycode_java8.nice3; import airycode_java8.nice1.Employee; import org.junit.Test; import ja ...

  3. java.lang.NoClassDefFoundError: org/hibernate/QueryTimeoutException

    在做ssh整合的时候报错:java.lang.NoClassDefFoundError: org/hibernate/QueryTimeoutException org.springframework ...

  4. 有关g++的Xlinker选项

    事情的起因是,在使用Eclipse配置opencv的库时,发现使用-Wl 选项需要自己在Linker flags的输入框里指定.并不是像使用-l选项或-I选项那样可以直接添加选项的条目就可以了.但是在 ...

  5. pdf转txt

    ubuntu pdf转jpg或txt chenlei posted @ 2009年12月30日 17:22 inLinux , 1818 阅读 呵呵,刚刚在网上定购了一款mp5,后来才发现它不支持PD ...

  6. 4.GDscript(2)关键字,运算符,字面量

    (来源godot官方文档) 关键词 下面是该语言支持的关键字列表.由于关键字是保留字(令牌),它们不能用作标识符.操作符(如 in , not , and 或 or )以及下面列出的内置类型的名称也是 ...

  7. Chess (SG + 状态压缩预处理)

    #include<bits/stdc++.h> #define bit(t) (1 << t) using namespace std; <<; ;//k是集合s的 ...

  8. python pprint

    使用 pprint 模块 pprint 模块( pretty printer ) 用于打印 Python 数据结构. 当你在命令行下打印特定数据结构时你会发现它很有用(输出格式比较整齐, 便于阅读). ...

  9. Hive 的排名和跨行 窗口函数及其使用

    一.排序&去重分析 row_number() over(partititon by col1 order by col2) as rn 也可以用 row_number() over(distr ...

  10. 栈(stack)和堆(heap)

    栈(stack)和堆(heap), Java程序在运行时都要开辟空间,任何软件在运行时都要在内存中开辟空间,Java虚拟机运行时也是要开辟空间的.JVM运行时在内存中开辟一片内存区域,启动时在自己的内 ...