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包下ExpectExceptionFailFailOnTimeOutInvokeMethodRunAftersRunBefores
// @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 ...
随机推荐
- Python3制作中文词云图
1. 准备好文本数据 2. pip install jieba 3. pip install wordcloud 4. 下载字体例如Songti.ttc(mac系统下的称呼,并将字体放在项目文件夹下) ...
- MacBook Air 2014 安装win7
1.准备一个4G以上容量USB3.0 U盘.制作一个带USB3.0驱动的win7 2.将制作好的win7iso镜像文件复制到macbook上,插上U盘,运行Boot Camp助理: 3.选择默认勾选项 ...
- buildroot 修改root密码后无法登录ssh解决方法
客户说想修改root密码后再登录ssh, 研究了一下,是因为ssh登录是匹配了之前的 密码生成文件,只要把之前的密码生成文件删除就可以. 过程如下: 删除 /etc/ssh/ssh_host*. rm ...
- hive中关键字作为列名的方法
hive中有很多关键字,直接作为列名,会出错的 例如 下面 user就是关键字,作为字段时报以下错误. 解决方案: 使用·· (ESC下面的那个键,点号)两个符号包裹即可.
- [转]Python中函数的值传递和引用传递
首先还是应该科普下函数参数传递机制,传值和传引用是什么意思? 函数参数传递机制问题在本质上是调用函数(过程)和被调用函数(过程)在调用发生时进行通信的方法问题.基本的参数传递机制有两种:值传递和引用传 ...
- 删除Cookies
网上一堆删除Cookies的JS代码 但是都删不了,找了好久终于在MSDN的论坛找到一个方法 function ClearCookie(name) { ****; var expDate = new ...
- 29个酷炫的Firefox配置参数
你可能安装了许多的firefox插件以增加浏览器的功能,但是如果你想更好地使用firefox,学习如何配置about:config参数是很有必要的. about:config配置页包含了所有的fire ...
- Entity Framework中的实体类添加复合主键
使用Code First模式实现给实体类添加复合主键,代码如下: using System; using System.Collections.Generic; using System.Compon ...
- 关于Unity的开发模式
Unity是组件化的开发模式,总结起来就是节点与组件.节点就像人,组件就像工具,人拿不同的工具,就变成不同的角色,有不同的作用.人+医学常识=医生. 1.每个空节点创建后,刚开始只有一个Transfo ...
- 为什么选择使用Spring Cloud而放弃了Dubbo
为什么选择使用Spring Cloud而放弃了Dubbo 可能大家会问,为什么选择了使用Dubbo之后,而又选择全面使用Spring Cloud呢?其中有几个原因: 1)从两个公司的背景来谈:Dubb ...