JUnit4运行过程中,org.junit.runner.notification. RunListener和RunNotifier运用了观察者模式

1.观察者

观察者Observer/Listener主要作用是分析各种事件并定义对应的回调接口

比如JDK中MouseListener处理鼠标键相关的5个动作:鼠标键被按下/pressed、释放/released、单击/clicked、光标进入或离开某组件/enters or exits。java.awt.event .MouseListener的源码:

public interface MouseListener extendsEventListener {

publicvoid mouseClicked(MouseEvent e);

publicvoid mousePressed(MouseEvent e);

publicvoid mouseReleased(MouseEvent e);

publicvoid mouseEntered(MouseEvent e);

publicvoid mouseExited(MouseEvent e);

}

那么。RunListener处理測试执行的7个动作

1. publicvoid testRunStarted(Description description)

在全部測试将要执行前的动作。如同运动会比赛前召开开幕式一样。

2. public void testStarted(Description description)

在一个測试(如@Test)開始之前的动作。

3. public void testFinished(Description description)

相应testStarted。一个測试结束后的动作。不论測试succeeds or fails。

4.public void testRunFinished(Result
result)

相应testRunStarted,全部測试执行后的动作。

5.public void testIgnored(Description description)

遇到一个@Ignore測试方法是的动作。

6. public void testFailure(Failure
failure)

若測试失败。调用这个动作。

7. public void testAssumptionFailure(Failure failure)

与断言不同,Assume定义了4个静态的測试条件,如assumeTrue(boolean b)等。

假设条件不满足时调用本方法。

RunListener定义了这7个空方法,地位等同于MouseAdapter,yqj2065认为不妨用abstract修饰它。

注意:回调接口的參数,用于将数据传递给上层模块。因为7个动作发生的时机不同,RunListener中使用了Description、Failure和Result封装回调接口所需的数据。

org.junit.runner.notification.Failure封装的数据有:final Description、final Throwable。

Result封装的数据有:

privateAtomicInteger fCount // the number of tests run

privateAtomicInteger fIgnoreCount// the number of tests ignored

privatefinal List<Failure> fFailures

privatelong fRunTime// milliseconds for run the entire suite

privatelong fStartTime;

Result有一些get方法,还提供了几个便利方法如

public booleanwasSuccessful() //fFailures .size()为0f返回true

另一个自带的私有内部类Listener,用于产生Result封装的数据。比如

public voidtestFinished(Description description) throws Exception {

fCount.getAndIncrement();

}

把这个代码放在testStarted中也能够。

(能够删除这些类型)

2. TextListener

详细监听器org.junit.internal.TextListener将以打印文本的形式处理7种动作。

正如我们经常使用的System.out.println()。TextListener的打印工作由一个java.io.PrintStream完毕,而该对象由System或JUnitSystem指定。顺便说明接口JUnitSystem有两个方法:exit(int i)和PrintStream out();其子类RealSystem代码

public class RealSystem implements JUnitSystem {

publicvoid exit(int code) {   System.exit(code);  }

publicPrintStream out() {            returnSystem.out;  }

}

TextListener为编写我们自己的Listener提供了一个简单的样例。(能够删除这些类型)

3. RunNotifier

被观察目标Subject/Notifier,某种事件发生或数据/状态改变后,自己主动调用doNotify()转而调用回调。RunNotifier是一个半截子的Subject,它维护一个注冊表List<RunListener>。有addListener、removeListener操作;可是它的7个fireXxx方法触发对回调接口的调用,不涉及某种事件发生或数据/状态改变。

这就是典型的二传手式委派

真正的幕后的Subject是谁呢?

因此这个二传手是一个孤零零的类,没有子类,全部public方法都在凝视中声称为Internaluse only。

本文涉及的类型:org.junit.runner.notification.RunListener及其子类org.junit.internal.TextListener(JUnitSystem和RealSystem)、数据Description、Failure和Result、RunNotifier

涉及的设计模式:观察者模式。

最新文章

  1. JAVA编程讲座-吴老
  2. FastSocket.Net
  3. MSSQL数字时间(timestamp)转换为DATETIME
  4. BZOJ 1787: [Ahoi2008]Meet 紧急集合( 树链剖分 )
  5. Git 的 .gitignore 配置 转载
  6. QT 的使用及编写代码遇到的问题和解决方法
  7. Silverlight——施工计划日报表(一)
  8. Python 并发编程(一)之线程
  9. ES6 中 Promise 详解
  10. 【zabbix教程系列】六、自动注册(Linux)
  11. 全文搜索引擎 ElasticSearch 还是 Solr?
  12. Luogu P3868 [TJOI2009]猜数字
  13. ESD选型指南
  14. RabbitMQ三种Exchange模式(fanout,direct,topic)的性能比较
  15. [Functional Programming] Functional JS - Pointfree Logic Functions
  16. poj-3667(线段树区间合并)
  17. linux虚拟机安装中出现的问题
  18. Python——eventlet.websocket
  19. C++ 判断当前系统x64 or x86
  20. 基于Naive Bayes算法的文本分类

热门文章

  1. 深刻理解Java中的String、StringBuffer和StringBuilder的差别
  2. ubuntu下无法将iNode绑定到侧边栏的解决办法
  3. ROS-机器人建模与仿真概论
  4. Linux 系列- 基本命令
  5. 高并发之后端优化(PHP)
  6. 微信小程序中获取高度及设备的方法
  7. 关于C语言中EOF的一点认识
  8. Clonezilla制作镜像时报错: errextfsclone.c:bitmap free count err
  9. luoguP4238 【模板】多项式求逆 NTT
  10. EntityFramework 二