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的更多相关文章

  1. JUnit4.12 源码分析之TestClass

    1. TestClass // 源码:org.junit.runners.model.TestClass // 该方法主要提供方法校验和注解搜索 public class TestClass impl ...

  2. JUnit4.12 源码分析(二)之TestRule

    1. TestRule TestRule和@Before,@After,@BeforeClass,@AfterClass功能类似,但是更加强大; JUnit 识别TestRule的两种方式: 方法级别 ...

  3. 【JUnit4.10源码分析】5 Statement

    假设要评选JUnit中最最重要的类型.或者说核心,无疑是org.junit.runners.model.Statement.Runner等类型看起来热闹而已. package org.junit.ru ...

  4. 【JUnit4.10源码分析】6.1 排序和过滤

    abstract class ParentRunner<T> extends Runner implements Filterable,Sortable 本节介绍排序和过滤. (尽管JUn ...

  5. 12.源码分析—如何为SOFARPC写一个序列化?

    SOFARPC源码解析系列: 1. 源码分析---SOFARPC可扩展的机制SPI 2. 源码分析---SOFARPC客户端服务引用 3. 源码分析---SOFARPC客户端服务调用 4. 源码分析- ...

  6. MyBatis 源码分析——生成Statement接口实例

    JDBC的知识对于JAVA开发人员来讲在简单不过的知识了.PreparedStatement的作用更是胸有成竹.我们最常见用到有俩个方法:executeQuery方法和executeUpdate方法. ...

  7. 【JUnit4.10源码分析】5.2 Rule

    标注@Rule TestRule是一个工厂方法模式中的Creator角色--声明工厂方法. package org.junit.rules; import org.junit.runner.Descr ...

  8. 【JUnit4.10源码分析】3.4 Description与測试树

    Description使用组合模式描写叙述一个測试树.组合模式中全部元素都是Composite对象. Description有成员变量private final ArrayList<Descri ...

  9. Solr4.8.0源码分析(12)之Lucene的索引文件(5)

    Solr4.8.0源码分析(12)之Lucene的索引文件(5) 1. 存储域数据文件(.fdt和.fdx) Solr4.8.0里面使用的fdt和fdx的格式是lucene4.1的.为了提升压缩比,S ...

随机推荐

  1. Python删除列表中元素

    Python中列表(list)是很常用的数据结构,删除列表中的元素有几种方法 列表的remove方法 lst = [1, 1, 3, 4] lst.remove(1) # lst->[1, 3, ...

  2. FreeRTOS 系统时钟节拍和时间管理

    以下转载自安富莱电子: http://forum.armfly.com/forum.php FreeRTOS 的时钟节拍任何操作系统都需要提供一个时钟节拍,以供系统处理诸如延时. 超时等与时间相关的事 ...

  3. 插入节点appendChild()

    http://www.imooc.com/code/1698 插入节点appendChild() 在指定节点的最后一个子节点列表之后添加一个新的子节点. 语法: appendChild(newnode ...

  4. [C++]红色波浪线是什么意思

    相关资料:https://zhidao.baidu.com/question/242005953.html 问题现象:在写C++代码时,写的注释都是红色波浪线. 问题原因:波浪线表示 词语拼写错误 字 ...

  5. 基于jQuery实现文字倾斜显示代码

    这是一款基于jQuery实现文字倾斜显示,这是一款基于jQuery实现的超酷动态文字显示效果.适用浏览器:IE8.360.FireFox.Chrome.Safari.Opera.傲游.搜狗.世界之窗. ...

  6. C语言 · 逆序排列

    算法提高 逆序排列   时间限制:1.0s   内存限制:512.0MB      问题描述 编写一个程序,读入一组整数(不超过20个),并把它们保存在一个整型数组中.当用户输入0时,表示输入结束.然 ...

  7. Linux samba 服务的配置

    今天有个学生问我 samba 服务怎么配置,所以晚上特意研究一下怎么配置这个服务. 过程如下: sudo apt-get install samba samba-common // 安装 samba ...

  8. jQuery 中 attr() 和 prop() 方法的区别<转>

    前几天,有人给 Multiple Select 插件 提了问题: setSelects doesn't work in Firefox when using jquery 1.9.0 一直都在用 jQ ...

  9. php -- 判断文件是否存在

    file_exists is_file is_dir 基本上,PHP的 file_exists = is_dir + is_file 写程序验证一下: 分别执行1000次,记录所需时间. ------ ...

  10. 【NOIP模拟题】Permutation(dp+高精度)

    首先我们可以这样想: 设状态f[i, j]表示1-i序列有j个'<'的方案数 那么考虑转移 因为i比i-1大,所以可以考虑从i-1来转移.首先i是要插入1-i-1这个序列的,所以我们可以思考插入 ...