junit4X系列--Exception
2016-02-05 22:15
330 查看
原文出处:http://www.blogjava.net/DLevin/archive/2012/11/02/390684.html。感谢作者的无私分享。
说来惭愧,虽然之前已经看过JUnit的源码了,也写了几篇博客,但是长时间不写Test Case,今天想要写抛Exception相关的test case时,竟然不知道怎么写了。。。。。好记性不如烂笔头,记下来先~~
对于使用验证Test Case方法中抛出的异常,我起初想到的是一种比较简单的方法,但是显得比较繁琐:
@Test
@Test(expected = IllegalStateException.class)
@Test(expected = IllegalStateException.class)
@Rule
public final ExpectedException expectedException = ExpectedException.none();
@Test
@Test
public void testThrowExceptionAndMessageWithRule() {
expectedException.expect(IllegalStateException.class);
expectedException.expectMessage("fail");
throw new IllegalStateException("expect
fail");
}
在stackoverflow中还有人提到了使用google-code中的catch-exception工程,今天没时间看了,回去好好研究一下。地址是:http://code.google.com/p/catch-exception/
说来惭愧,虽然之前已经看过JUnit的源码了,也写了几篇博客,但是长时间不写Test Case,今天想要写抛Exception相关的test case时,竟然不知道怎么写了。。。。。好记性不如烂笔头,记下来先~~
对于使用验证Test Case方法中抛出的异常,我起初想到的是一种比较简单的方法,但是显得比较繁琐:
@Test
@Test(expected = IllegalStateException.class)
@Test(expected = IllegalStateException.class)
@Rule
public final ExpectedException expectedException = ExpectedException.none();
@Test
@Test
public void testThrowExceptionAndMessageWithRule() {
expectedException.expect(IllegalStateException.class);
expectedException.expectMessage("fail");
throw new IllegalStateException("expect
fail");
}
在stackoverflow中还有人提到了使用google-code中的catch-exception工程,今天没时间看了,回去好好研究一下。地址是:http://code.google.com/p/catch-exception/
相关文章推荐
- 第五章 JVM垃圾收集器(1)
- 数据集参数取值构件
- HDU-1253 胜利大逃亡 (BFS)
- junit4X系列--Builder、Request与JUnitCore
- iOS-对话框的应用
- java中基本类型的参数传递和引用类型的参数传递
- 安卓开发——详解camera.rotate(x,y,z);的旋转方向
- junit4X系列--Builder、Request与JUnitCore
- C 封装一个通用链表 和 一个简单字符串开发库
- 基于OpenWrt的PPTP插件开启防火墙转发
- poj 2079 Triangle(旋转卡壳)
- junit4X系列--Rule
- Codeforces 514E 矩阵快速幂
- junit4X系列--Rule
- [原创][LaTex]汇总博文
- uva 10859 树形dp
- HDU2824 The Euler function(欧拉函数)
- LeetCode322. Coin Change
- 接口1
- jQuery2(JQuery实现onload)