【JUnit4.10源码分析】5.2 Rule
标注@Rule
TestRule是一个工厂方法模式中的Creator角色——声明工厂方法。
package org.junit.rules;
import org.junit.runner.Description;
import org.junit.runners.model.Statement;
public interface TestRule {
Statement apply(Statement base, Description description);
}
因为工厂方法apply有參数base。因而TestRule将创建装饰模式中的装饰对象。
【抽象类Statement声明操作evaluate()的接口。它作为一个回调接口,上层模块能够定义各种Statement的子类。提供evaluate()的方法体。而这一主要的技术与Rule结合,成为JUnit一个很重要的手段——能够说它是一个通用型的复合命令的构造方式。全然能够代替Statement的一些复合命令的子类如ExpectException等。
】
測试程序猿要使用Rule,必须编写代码。以下先介绍使用Rule的样例。
1.MyStatement——Statement的一个新的装饰对象。( JUnit之Rule的使用中的样例),地位等同于ExpectException。
package rule;
import static tool.Print.*;//pln(Object)
import org.junit.runners.model.Statement;
/**
*
* @author yqj2065
*/
public class MyStatement extends Statement {
private final Statement base;
public MyStatement( Statement base ) {
this.base = base;
} @Override public void evaluate() throws Throwable {
pln( "before...sth..sth" );
try {
base.evaluate();
} finally {
pln( "after...sth..sth" );
}
}
}
2.一个详细的工厂。也就是定义工厂方法的TestRule的子类。MyRule将创建一个Statement的新的装饰对象MyStatement。如同工厂方法模式的一般使用方法,详细工厂与详细产品一一相应。
package rule;
import org.junit.rules.TestRule;
import org.junit.runners.model.Statement;
import org.junit.runner.Description;
public class MyRule implements TestRule {
@Override
public Statement apply(Statement base, Description description) {
return new MyStatement( base );
}
}
3.使用新的装饰对象
用户类BlockJUnit4ClassRunner使用的是ExpectException等JUnit内建的Statement装饰对象;測试程序猿编写的Statement装饰对象,则须要通过@Rule,嵌入到单元測试类中。
package rule;
import org.junit.Rule;
import org.junit.Test;
public class MyTest {
@Rule
public MyRule myRule = new MyRule(); @Test()
public void xx() {
System.out.println( "xx()..." );
}
}
在开发环境中执行MyTest,输出:
before...sth..sth
xx()...
after...sth..sth
例程 8 17标注Rule
package org.junit;
import java.lang.annotation.*;
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.FIELD})
public @interface Rule {}
注意:JUnit4.8中Rule标注并没有指定@Target(ElementType.FIELD),而4.10版本号中明白定义出来。Rule用于修饰測试单元类中一个特殊的域。该域必须是public、非static并且是org.junit.rules.TestRule子类型的引用变量(代替4.8版本号的org.junit.rules.MethodRule)。JUnit4.10版本号中还有一个标注@ClassRule,用于修饰測试单元类中static的TestRule子类型的引用变量。
TestRule
然而,装饰对象仍然是一个Statement。
TestRule的各种实现类创建很多装饰对象。可是没有特别的名字。反正是一个Statement。
从JUnit框架设计的角度看,TestRule是工厂方法;而从測试程序猿思考问题的角度,TestRule及事实上现类,为单元測试制订了各种附加的规则/Rule。
在org.junit.rules包中的TestRule的实现类有:
1. TestWatcher
对base加以监控的Rule。它不会对结果进行改动。
TestWatcher以匿名类的方式创建Statement装饰对象。在@Override evaluate()时使用了模板方法模式。
例程 8-4 TestWatchman
package org.junit.rules; import org.junit.internal.AssumptionViolatedException;
import org.junit.runner.Description;
import org.junit.runners.model.Statement;
public abstract class TestWatcher implements TestRule {
public Statement apply(final Statement base, final Description description) {
return new Statement() {
@Override
public void evaluate() throws Throwable {
starting(description);
try {
base.evaluate();
succeeded(description);
} catch (AssumptionViolatedException e) {
throw e;
} catch (Throwable t) {
failed(t, description);
throw t;
} finally {
finished(description);
}
}
};
}
protected void succeeded(Description description) { }
protected void failed(Throwable e, Description description) { }
protected void starting(Description description) { }
protected void finished(Description description) { }
}
TestWatcher的子类TestName是模板方法模式中的详细类角色。
publicvoid starting(FrameworkMethod method) {
fName=method.getName();
}
2. Verifier
使用了模板方法模式,以匿名类的方式创建Statement装饰对象,在base. evaluate()之后加入一个verify()操作。Verifier的子类ErrorCollector收集測试中的错误。
3. ExpectedException和Timeout
ExpectedException作为工厂创建装饰对象,其私有内部类ExpectedExceptionStatement全然等价于org.junit.internal.runners.statements.ExpectException的地位。可是,ExpectedException作为规则将作用于各种測试方法。而ExpectException作用于@Test(expected=xxx)修饰的方法。
Timeout则是典型的工厂。返回一个装饰对象FailOnTimeout。
package org.junit.rules;
import org.junit.internal.runners.statements.FailOnTimeout;
import org.junit.runner.Description;
import org.junit.runners.model.Statement; public class Timeout implements TestRule {
private final int fMillis; /**
* @param millis the millisecond timeout
*/
public Timeout(int millis) {
fMillis= millis;
} public Statement apply(Statement base, Description description) {
return new FailOnTimeout(base, fMillis);
}
}
4. ExternalResource
ExternalResource使用了模板方法模式。以匿名类的方式创建Statement装饰对象。在base. evaluate()之前before()中能够加入各种其它资源(file, socket, server, database connection),而after()中释放资源。其子类TemporaryFolder给出了一个样例。
【JUnit4.10源码分析】5.2 Rule的更多相关文章
- 【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 ...
- 【JUnit4.10源码分析】3.4 Description与測试树
Description使用组合模式描写叙述一个測试树.组合模式中全部元素都是Composite对象. Description有成员变量private final ArrayList<Descri ...
- JUnit源码分析 - 扩展 - 自定义Rule
JUnit Rule简述 Rule是JUnit 4.7之后新加入的特性,有点类似于拦截器,可以在测试类或测试方法执行前后添加额外的处理,本质上是对@BeforeClass, @AfterClass, ...
- JUnit4.12 源码分析之TestClass
1. TestClass // 源码:org.junit.runners.model.TestClass // 该方法主要提供方法校验和注解搜索 public class TestClass impl ...
- 10.源码分析---SOFARPC内置链路追踪SOFATRACER是怎么做的?
SOFARPC源码解析系列: 1. 源码分析---SOFARPC可扩展的机制SPI 2. 源码分析---SOFARPC客户端服务引用 3. 源码分析---SOFARPC客户端服务调用 4. 源码分析- ...
- JUnit4.12 源码分析之Statement
1. Statement 抽象类Statement作为命令模式的Command,只有一个方法 各种Runner作为命令模式中的Invoker,将发出各种Statement,来表示它们运行JUnit测试 ...
- JUnit4.12 源码分析(二)之TestRule
1. TestRule TestRule和@Before,@After,@BeforeClass,@AfterClass功能类似,但是更加强大; JUnit 识别TestRule的两种方式: 方法级别 ...
- 11.源码分析---SOFARPC数据透传是实现的?
先把栗子放上,让大家方便测试用: Service端 public static void main(String[] args) { ServerConfig serverConfig = new S ...
随机推荐
- 计算两端yuv视频流中每一帧的ssim值
方法同上一篇,仅仅不多这里在计算的时候用了opencv1的接口,出现了一些问题.最后总算攻克了. 程序: #include <stdlib.h> #include <stdio.h& ...
- 使用mysql 统计函数 结果为null时返回值改为0
SELECT COALESCE(SUM(total),0) FROM test_table
- 学习Struts框架系列(三):声明式异常处理
在Struts1.X的版本中加入了对异常的处理Exception Handler,有了它我们可以不使用try/catch捕获异常,一旦出现了我们已经定义的异常,那么就会转到相应的页面,并且携带异常信息 ...
- 算法笔记_209:第六届蓝桥杯软件类决赛部分真题(Java语言B组)
目录 1 分机号 2 五星填数 3 表格计算 前言:以下代码仅供参考,若有错误欢迎指正哦~ 1 分机号 标题:分机号 X老板脾气古怪,他们公司的电话分机号都是3位数,老板规定,所有号码必须是降序排列, ...
- Windows安装MySQL解压版
1:解压 2:设置环境变量 3:修改my.ini [mysqld] basedir = D:\MySQL\Server\mysql--win32 datadir = D:\MySQL\Server\d ...
- javaScriptObject转String
function obj2str(o){ var r = []; if(typeof o =="string") return "\""+o.repl ...
- Zuul使用Ribbon配置自动重试
spring cloud的版本不断演进,导致很多配置的配置方式不断改变,有时某个配置在一个版本里面默认是true,后边一升级默认成了false,这点让人有点不爽. 言归正传 0.所使用版本 sprin ...
- scala class和object,trait的区别
Scala类 1 2 3 4 5 6 7 8 9 10 11 12 13 class Counter { private var value = 0 // 必须初始化字段 def increm ...
- DevExpress GridControl 动态创建字段及主细关系表过程
原文地址:http://hi.baidu.com/qdseashore/item/38f1153e9d0143637d034b7a 在做项目中,往往需要在查询基类模板窗口内做主细关系Grid,引用一下 ...
- Linux安装SQLite轻量级数据库
SQLite,是一款轻型的数据库,是遵守ACID的关系型数据库管理系统,它包含在一个相对小的C库中.它是D.RichardHipp建立的公有领域项目.它的设计目标是嵌入式的,而且目前已经在很多嵌入式产 ...