JUnit4.12 源码分析之Statement
1. Statement
- 抽象类Statement作为命令模式的Command,只有一个方法
- 各种
Runner
作为命令模式中的Invoker
,将发出各种Statement,来表示它们运行JUnit测试的整个过程; org.junit.internal.runners.statement
包中定义了Statement的子类(具体命令),来处理针对方法
的标注,如@Test
,@Before
,@After
,@BeforeClass
,@AfterClass
;
// org.junit.runners.model.Statement
public abstract class Statement{
public abstract void evalute() throws Throwable;
}
// BlockJUnit4ClassRunner 中的符合命令,来处理 @Test, @Before, @After...
// org.junit.runners.BlockJUnit4ClassRunner
public class BlockJUnit4ClassRunner extends ParentRunner<FrameworkMethoc>{
...(略)
protected Statement methodBlock(FrameworkMethod method){
Object test;
try{
test = new ReflectiveCallable(){
@Override
protected Object runReflectiveCall() throws Throwable{
return createTest();
}
}.run();
}catch(Throwable e){
return new Fail(e);
}
// 各种Statement
Statement statement = methodInvoker(method, test);
statement = possiblyExpectingExceptions(method, test, statement);
statement = withPotentialTimeout(method, test, statement);
statement = withBefores(method, test, statement);
statement = withAfters(method, test, statement);
statement = withRules(method, test, statement);
return statement;
}
// 根据反射,执行无参构造函数
protected Object createTest() throws Exception{
return getTestClass().getOnlyConstructor().newInstance();
}
// Statement builders
protected Statement methodInvoker(FrameworkMethod method, Object test){
return new InvokeMethod(method, test);
}
}
2.Statement 的实现类
org.junit.internal.runners.statements
包下ExpectException
Fail
FailOnTimeOut
InvokeMethod
RunAfters
RunBefores
// @Test(expected=IndexOutOfBoundsException.class)
// 源码 org.junit.internal.runners.statements.ExpectException
public class ExpectException extends Statement{
private final Statement next;
private final Class<? extends Throwable> expected;
public ExpectException(Statement next, Class<? extends Throwable> expected){
this.next = next;
this.expected = expected;
}
@Override
public void evalute() throws Exception{
boolean complete = false;
try{
next.evalute();
complete = true;
}catch(AssumptionViolatedException e){
throw e;
}catch(Throwable e){
if(!expected.isAssignableFrom(e.getClass())){
String message = "Unexpected exception, expected<"
+ expected.getName() + "> but was<"
+ e.getClass().getName() + ">";
throw new Exception(messge, e);
}
}
if(complete){
throw new AssertionError("Expected exception: "
+ expected.getName());
}
}
}
参考资料:
JUnit4.12 源码分析之Statement的更多相关文章
- JUnit4.12 源码分析之TestClass
1. TestClass // 源码:org.junit.runners.model.TestClass // 该方法主要提供方法校验和注解搜索 public class TestClass impl ...
- JUnit4.12 源码分析(二)之TestRule
1. TestRule TestRule和@Before,@After,@BeforeClass,@AfterClass功能类似,但是更加强大; JUnit 识别TestRule的两种方式: 方法级别 ...
- 【JUnit4.10源码分析】5 Statement
假设要评选JUnit中最最重要的类型.或者说核心,无疑是org.junit.runners.model.Statement.Runner等类型看起来热闹而已. package org.junit.ru ...
- 【JUnit4.10源码分析】6.1 排序和过滤
abstract class ParentRunner<T> extends Runner implements Filterable,Sortable 本节介绍排序和过滤. (尽管JUn ...
- 12.源码分析—如何为SOFARPC写一个序列化?
SOFARPC源码解析系列: 1. 源码分析---SOFARPC可扩展的机制SPI 2. 源码分析---SOFARPC客户端服务引用 3. 源码分析---SOFARPC客户端服务调用 4. 源码分析- ...
- MyBatis 源码分析——生成Statement接口实例
JDBC的知识对于JAVA开发人员来讲在简单不过的知识了.PreparedStatement的作用更是胸有成竹.我们最常见用到有俩个方法:executeQuery方法和executeUpdate方法. ...
- 【JUnit4.10源码分析】5.2 Rule
标注@Rule TestRule是一个工厂方法模式中的Creator角色--声明工厂方法. package org.junit.rules; import org.junit.runner.Descr ...
- 【JUnit4.10源码分析】3.4 Description与測试树
Description使用组合模式描写叙述一个測试树.组合模式中全部元素都是Composite对象. Description有成员变量private final ArrayList<Descri ...
- Solr4.8.0源码分析(12)之Lucene的索引文件(5)
Solr4.8.0源码分析(12)之Lucene的索引文件(5) 1. 存储域数据文件(.fdt和.fdx) Solr4.8.0里面使用的fdt和fdx的格式是lucene4.1的.为了提升压缩比,S ...
随机推荐
- CM本地Yum源的搭建
CM本地Yum源的搭建 以本地yum源安装CM5为例,解释本地yum源的安装和利用本地yum源安装CM5. Cloudera Manager 5(以下简称CM)默认采用在线安装的方式,给不能联互联网或 ...
- Django接受ajax传过来的数组
$.ajax({ cache: false, type: "POST", url: "/userdelete/", traditional:true, //加上 ...
- dropload.js下拉加载更多
项目中有用到下拉加载更多的地方,去网上找了一个插件,地址:http://ons.me/526.html总体还是不错的,可能自己不是特别了解这个插件,做项目时,也是遇到了无数问题.项目中要用的是两个ta ...
- C语言 · 素数判断
算法提高 素数判断 时间限制:1.0s 内存限制:512.0MB 编写一函数IsPrime,判断某个大于2的正整数是否为素数. 样例输入: 5样例输出:yes 样例输入: 9样例输 ...
- C++ operator关键字(重载操作符)
operator是C++的关键字,它和运算符一起使用,表示一个运算符函数,理解时应将operator=整体上视为一个函数名. 这是C++扩展运算符功能的方法,虽然样子古怪,但也可以理解:一方面 ...
- ssh配置authorized_keys后仍然需要输入密码的问题
前阵子搭建Hadoop时,配置了本机(localhost)的ssh的公钥到authorized_keys文件中,但是在ssh连接localhost时仍然提示需要输入密码,后来发现是$HOME/.ssh ...
- C51寄存器详解(Reg51.h)
Reg51.h 这个头文件将C程序中能用到的寄存器名或寄存器中某位的名称与硬件地址值做了对应,在程序中直接写出这些名称,集成开发环境就能识别,并最终转换成机器代码,实现对单片机各硬件资源的准确操控. ...
- Hibernate Annotation 字段 默认值
http://emavaj.blog.163.com/blog/static/133280557201032262741999/ ——————————————————————————————————— ...
- 让 MySQL 支持 emoji 存储
要让 MySQL 开启 utf8mb4 支持,需要一些额外的设置. 1. 检查 MySQL Server 版本 utf8mb4 支持需要 MySQL Server v5.5.3+ 2. 设置表的 CH ...
- MongoDB助力快速搭建物流订单系统
简介 快递物流系统里最常见的一种业务类型就是订单的查询和记录.订单的特点是随着递送过程,订单数据需要随时更新路径.数据结构上需要可以灵活应对,这点非常符合Document模型,并且MongoDB支持G ...