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

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

JUnit4.8.2源码分析-4 RunNotifier与RunListener的更多相关文章

  1. JUnit4.8.2源码分析-1 说明

    阅读本系列文章时须要知道的: JUnit是由GOF 之中的一个的Erich Gamma和 Kent Beck 编写的一个开源的单元測试框架,分析JUnit源码的主要目的是学习当中对设计模式的运用.JU ...

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

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

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

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

  4. JUnit4.12 源码分析之TestClass

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

  5. JUnit源码分析 - 扩展 - 自定义Rule

    JUnit Rule简述 Rule是JUnit 4.7之后新加入的特性,有点类似于拦截器,可以在测试类或测试方法执行前后添加额外的处理,本质上是对@BeforeClass, @AfterClass, ...

  6. JUnit源码分析 - 扩展 - 自定义RunListener

    RunListener简述 JUnit4中的RunListener类用来监听测试执行的各个阶段,由RunNotifier通知测试去运行.RunListener与RunNotifier之间的协作应用的是 ...

  7. 【异常及源码分析】org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.type.TypeException: Could not set parameters for mapping: ParameterMapping

    一.异常出现的场景 1)异常出现的SQL @Select("SELECT\n" + " id,discount_type ,min_charge, ${cardFee} ...

  8. Junit 3.8.1 源码分析(一)

    写在前面:本文基于Junit3.8.1版本,因为这是我第一次进行源码学习,先从简单的源码开始学起 1. 示例代码 1.1 准备工作 下载Junit3.8.1的JAR包 需要下载junit-3.8.1- ...

  9. ABP源码分析一:整体项目结构及目录

    ABP是一套非常优秀的web应用程序架构,适合用来搭建集中式架构的web应用程序. 整个Abp的Infrastructure是以Abp这个package为核心模块(core)+15个模块(module ...

随机推荐

  1. c11---位运算相关

    // // main.c // 03-原码反码补码 #include <stdio.h> int main(int argc, const char * argv[]) { // int占 ...

  2. Swift3.0 闭包整理

    语法表达式 一般形式:{             (parameters) -> returnType in              statements            } 这里的参数 ...

  3. 循环引用的weak和assgin

    __weak 当对象销毁后weakSelf指向的地址为nil __unsafe_unretained 挡圈对象销毁后,weakSelf执行的地址为不变,而地址对应对象已经销毁,再次访问该对象就崩溃,相 ...

  4. webform 下使用autofac

    官网介绍: http://docs.autofac.org/en/latest/integration/webforms.html#quick-start HTTP 错误 500.22 - Inter ...

  5. struts2学习之基础笔记8

    文件的上传和下载 上传 步骤1:在文件上传表单中设置method和enctype属性值 格式:<s:form method=”post” enctype =”multipart/ form.da ...

  6. 跟着8张思维导图学习javascript (转)

    学习的道路就是要不断的总结归纳,好记性不如烂笔头,so,下面将po出8张javascript相关的思维导图. 思维导图小tips:思维导图又叫心智图,是表达发射性思维的有效的图形思维工具 ,它简单却又 ...

  7. windows 2008 中IIS7.0以上如何设置404错误页面

    404错误页面的设置,不仅仅可以提高用户体验度,从SEO方面考虑,也是非常重要的.今天,笔者在这里介绍一下在windows 2008下如何设置404错误页面. 注意:设置404有我这里介绍2种方式,推 ...

  8. 硅谷最有名的帮派:如果你不知道PayPal黑帮

    paypal 你知道Tesla Motors,LinkedIn,SpaceX,Yelp,Yammer这几家公司的共同点吗?除了他们的市值都超过10亿美金之外,他们的创办人都是Paypal黑帮(Payp ...

  9. JQuery选择器排除某元素实现js代码

    使用JQuery选择器实现排除某一大元素下的某一元素的核心代码是使用.not()方法,如下所示: $("button").not("#save").attr(& ...

  10. 基础——(5)D Flip-Flop(D触发器)

    之前搞了一个 D-Latch,看一下下图是怎么变化的 In D-latch anytime its enabled the input D is going to be output at Q 使用c ...