junit4X系列--Exception
来源:互联网 发布:linux 局域网传文件 编辑:程序博客网 时间:2024/06/05 11:17
原文出处:http://www.blogjava.net/DLevin/archive/2012/11/02/390684.html。感谢作者的无私分享。
说来惭愧,虽然之前已经看过JUnit的源码了,也写了几篇博客,但是长时间不写Test Case,今天想要写抛Exception相关的test case时,竟然不知道怎么写了。。。。。好记性不如烂笔头,记下来先~~
对于使用验证Test Case方法中抛出的异常,我起初想到的是一种比较简单的方法,但是显得比较繁琐:
@Test
public void testOldStyle() {
try {
double value = Math.random();
if(value < 0.5) {
throw new IllegalStateException("test");
}
Assert.fail("Expect IllegalStateException");
} catch(IllegalStateException e) {
}
}
public void testOldStyle() {
try {
double value = Math.random();
if(value < 0.5) {
throw new IllegalStateException("test");
}
Assert.fail("Expect IllegalStateException");
} catch(IllegalStateException e) {
}
}
Google了一下,找到另外几种更加方便的方法:1,使用Test注解中的expected字段判断抛出异常的类型。2,使用ExpectedException的Rule注解。
个人偏好用Test注解中的expected字段,它先的更加简洁,不管读起来还是写起来都很方便,并且一目了然:
@Test(expected = IllegalStateException.class)
public void testThrowException() {
throw new IllegalStateException("test");
}
@Test(expected = IllegalStateException.class)
public void testNotThrowException() {
System.out.println("No Exception throws");
}
public void testThrowException() {
throw new IllegalStateException("test");
}
@Test(expected = IllegalStateException.class)
public void testNotThrowException() {
System.out.println("No Exception throws");
}
对Rule注解的使用(只有在JUnit4.7以后才有这个功能),它提供了更加强大的功能,它可以同时检查异常类型以及异常消息内容,这些内容可以只包含其中的某些字符,ExpectedException还支持使用hamcrest中的Matcher,默认使用IsInstanceOf和StringContains Matcher。在BlockJUnit4ClassRunner的实现中,每一个Test Case运行时都会重新创建Test Class的实例,因而在使用ExpectedException这个Rule时,不用担心在多个Test Case之间相互影响的问题:
@Rule
public final ExpectedException expectedException = ExpectedException.none();
@Test
public void testThrowExceptionWithRule() {
expectedException.expect(IllegalStateException.class);
throw new IllegalStateException("test");
}
@Test
public void testThrowExceptionAndMessageWithRule() {
expectedException.expect(IllegalStateException.class);
expectedException.expectMessage("fail");
throw new IllegalStateException("expect fail");
}
public final ExpectedException expectedException = ExpectedException.none();
@Test
public void testThrowExceptionWithRule() {
expectedException.expect(IllegalStateException.class);
throw new IllegalStateException("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/
0 0
- junit4X系列--Exception
- junit4X系列--Runner解析
- junit4X系列--Statement
- junit4X系列--Rule
- junit4X系列源码--总体介绍
- junit4X系列--Assert与Hamcrest
- Junit4X系列--hamcrest的使用
- junit4X系列--Builder、Request与JUnitCore
- junit4X系列源码--Junit4 Runner以及test case执行顺序和源代码理解
- C++基本功和 Design Pattern系列 Exception
- 【Fragment研究系列】Exception: commit already called
- Exception
- Exception
- Exception
- exception
- Exception
- Exception
- Exception
- 【LeetCode】9. Palindrome Number
- iOS-对话框的应用
- java中基本类型的参数传递和引用类型的参数传递
- 数据集参数取值构件
- html5实现滚动文字
- junit4X系列--Exception
- shrio 权限管理filterChainDefinitions过滤器配置
- JQuery3(map,each,trim方法)
- jQuery.extend函数详解--
- poj2001 Shortest Prefixes (trie树)
- 并查集
- AIM Tech Round (Div. 2) D. Array GCD(dp)
- POJ3694 Network(边双连通+LCA)
- xib自定义非等高的cell