JUnit4.12 源码分析(二)之TestRule
1. TestRule
TestRule
和@Before
,@After
,@BeforeClass
,@AfterClass
功能类似,但是更加强大;- JUnit 识别
TestRule
的两种方式:- 方法级别:
@Rule
; - 类级别:
@ClassRule
;
- 方法级别:
TestRule
的实现类:ErrorCollector
ExpectedException
ExternalResource
TemporaryFolder
TestName
TestWatcher
Timeout
Verifier
// org.juit.rules.TestRule 源码
public interface TestRule{
// 只有一个抽象方法
// 子类需要对Statement进行修改
// 返回值:可能是原来的base,或者装饰之后的base,或者一个全新的base
Statement apply(Statement base, Description description);
}
// org.junit.runners.model.Statement
public abstract class Statement{
// 只有一个抽象方法
public abstract void evalute() throws Throwable;
}
// org.junit.rules.ExpectedException
public class ExpectedException implements TestRule{
...(略)
// apply 方法的实现
public Statement apply(Statement base, Description description){
return new ExpectedExceptionStatement(base);
}
// 对Statement的处理
private class ExpectedExceptionStatement extends Statement{
private final Statement next;
public ExpectedExceptionStatement(Statement base){
next = base;
}
@Override
public void evalute() throws Throwable{
try{
next.evalute();
}catch(Throwable e){
handleException(e);
return;
}
if(isAnyExceptionExpected()){
failDueToMissingException();
}
}
}
...(略)
}
1.1 测试示例
// 自定义Statement
public class MyStatement extends Statement{
private final Statement myBase;
public MyStatement(Statement base){
this.myBase = base;
}
@Override
public void evalute() throws Throwable{
System.out.println("方法执行开始之前");
try{
myBase.evalute();
}finally{
System.out.println("方法执行之后");
}
}
}
// 自定义Rule
public class MyRule implements TestRule{
@Override
public Statement apply(Statement base, Description description){
return new MyStatement(base);
}
}
// 自定义Test
public class MyTest{
@Rule
public MyRule myRule = new MyRule();
@Test
public void testCase(){
System.out.println("测试运行....");
}
}
// 控制台输出:
方法执行开始之前
测试运行....
方法执行之后
参考资料:
JUnit之Rule的使用
JUnit4.12 源码分析(二)之TestRule的更多相关文章
- JUnit4.12 源码分析之TestClass
1. TestClass // 源码:org.junit.runners.model.TestClass // 该方法主要提供方法校验和注解搜索 public class TestClass impl ...
- JUnit4.12 源码分析之Statement
1. Statement 抽象类Statement作为命令模式的Command,只有一个方法 各种Runner作为命令模式中的Invoker,将发出各种Statement,来表示它们运行JUnit测试 ...
- 框架-springmvc源码分析(二)
框架-springmvc源码分析(二) 参考: http://www.cnblogs.com/leftthen/p/5207787.html http://www.cnblogs.com/leftth ...
- 多线程之美8一 AbstractQueuedSynchronizer源码分析<二>
目录 AQS的源码分析 该篇主要分析AQS的ConditionObject,是AQS的内部类,实现等待通知机制. 1.条件队列 条件队列与AQS中的同步队列有所不同,结构图如下: 两者区别: 1.链表 ...
- Fresco 源码分析(二) Fresco客户端与服务端交互(1) 解决遗留的Q1问题
4.2 Fresco客户端与服务端的交互(一) 解决Q1问题 从这篇博客开始,我们开始讨论客户端与服务端是如何交互的,这个交互的入口,我们从Q1问题入手(博客按照这样的问题入手,是因为当时我也是从这里 ...
- Tomcat源码分析二:先看看Tomcat的整体架构
Tomcat源码分析二:先看看Tomcat的整体架构 Tomcat架构图 我们先来看一张比较经典的Tomcat架构图: 从这张图中,我们可以看出Tomcat中含有Server.Service.Conn ...
- 十、Spring之BeanFactory源码分析(二)
Spring之BeanFactory源码分析(二) 前言 在前面我们简单的分析了BeanFactory的结构,ListableBeanFactory,HierarchicalBeanFactory,A ...
- Vue源码分析(二) : Vue实例挂载
Vue源码分析(二) : Vue实例挂载 author: @TiffanysBear 实例挂载主要是 $mount 方法的实现,在 src/platforms/web/entry-runtime-wi ...
- 【JUnit4.10源码分析】5 Statement
假设要评选JUnit中最最重要的类型.或者说核心,无疑是org.junit.runners.model.Statement.Runner等类型看起来热闹而已. package org.junit.ru ...
随机推荐
- widnows 使用WIN32 APi 实现修改另一打开程序的窗口显示方式
1.GUI点击打开一个程序那边做一个判断. hwnd = 获取目标程序窗口句柄: if(hwnd == NULL /*不存在目标程序窗口句柄*/){ 创建进程,打开目标程序: } else{ ...
- Mac 终端编译运行 C++
1.在编辑器中写好C++代码 2.打开终端打开文件对应的地址 3.用g++命令来编译.cpp文件 4.用./文件名来运行 观察文件的目录可发现 g++ 源文件名 编译源文件,产生a.out ./文件名 ...
- CSS布局奇淫技巧之--各种居中<转>
居中是我们使用css来布局时常遇到的情况.使用css来进行居中时,有时一个属性就能搞定,有时则需要一定的技巧才能兼容到所有浏览器,本文就居中的一些常用方法做个简单的介绍. 注:本文所讲方法除了特别说明 ...
- AtomicReference与volatile的区别
首先volatile是java中关键字用于修饰变量,AtomicReference是并发包java.util.concurrent.atomic下的类.首先volatile作用,当一个变量被定义为vo ...
- IOS让返回按钮显示自定义标题而不是上个ViewController的title
在开发IOS的时候发现当从一个ViewController跳转到另一个ViewController时,被跳转到的那个ViewController的返回按钮总是显示的是上个ViewController的 ...
- Java,Mysql-根据一个给定经纬度的点,进行附近500米地点查询–合理利用算法
Java,Mysql-根据一个给定经纬度的点,进行附近500米地点查询–合理利用算法 LBS 球面距离公式 http://wiki.myoa.info/zh-blog:20 Java,Mysql- ...
- [插件] 如何在一个页面中使用多个SWFUpload对象上传文件
首先需要引入相应的样式和JS文件,还需要借助jQuery的js 提供下载路径:http://pan.baidu.com/s/1EUzca ① 引入js <script type="te ...
- latex之图表位置控制
\begin{table}[!htbp] !-忽略“美学”标准 h-here t-top b-bottom p-page-of-its-own
- 错题0925-java
1.Given the following code: public class Test { private static int j = 0; private static Boolean met ...
- Statement和PreparedStatement都是用来发送和执行SQL语句的
Statement和PreparedStatement都是用来发送和执行SQL语句的 DriverManager管理一组驱动程序