JUnit4执行cases背后的故事(2)---默认执行器BlockJUnit4ClassRunner源码分析

(1)背景:

之前《JUnit4执行cases背后的故事(1)—JUnitCore源码分析》介绍了JUnit4执行测试用例默认的执行器是BlockJUnit4ClassRunner,它内部是怎么实现的呢? 本文将一一介绍。

(2)org.junit.runners.BlockJUnit4ClassRunner是什么?
官方解释如下:

it is now the default test class runner, but it should have exactly the same behavior as the old test class runner (JUnit4ClassRunner).

BlockJUnit4ClassRunner has advantages for writers of custom JUnit runners that are slight changes to the default behavior, however:

It has a much simpler implementation based on Statements, allowing new operations to be inserted into the appropriate point in the execution flow.
It is published, and extension and reuse are encouraged, whereas JUnit4ClassRunner was in an internal package, and is now deprecated.

概括来说:

BlockJUnit4ClassRunner是默认的测试类执行器,可以通过它方便地实现自定义执行器(可以在执行流中插入新的操作),它执行测试的方式是基于Statement block,Statement分成数种职责(BeforeClass, AfterClass,Before,After,执行测试方法等),不同的Statement负责自己的职责,执行结束后交给下一个Statement,直到所有Statement执行完毕;设计模式是职责链模式(Chain Of Responsibility Pattern)。

类继承关系:

java.lang.Object
  extended by org.junit.runner.Runner  
      extended by org.junit.runners.ParentRunner<FrameworkMethod>
          extended by org.junit.runners.BlockJUnit4ClassRunner
(3)源码分析

执行用例时,会执行runner.run(this.notifier);(见JUnit4执行cases背后的故事(1)—JUnitCore源码分析 第三部分),该runner为BlockJUnit4ClassRunner实例,调用其父类ParentRunner方法:

@Override
    public void run(final RunNotifier notifier) {
        EachTestNotifier testNotifier = new EachTestNotifier(notifier,
                getDescription());
        try {
            Statement statement = classBlock(notifier);
            statement.evaluate();
        } catch (AssumptionViolatedException e) {
            testNotifier.addFailedAssumption(e)
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值