简介
junit
基于java语言的单元测试类框架
Statement
对一个单元运行的封装,每个Statement都只是执行它本身所表达的逻辑,而将其他逻辑交给下一个Statement处理,而且基本上的Statement都存在对下一个节点的引用,从设计模式上讲 是一个职责连模式。JUnit中对@BeforeClass、@AfterClass、@Before、@After、@ClassRule、@Rule等逻辑就是通过Statement来实现的
【可以简单理解为:Statement代表了所有测试方法case集合,相当于我们需要执行的一系列任务,通过next指向下一个任务,实际就以链表形式存在】
junit和Statement的关系
junit是大的执行环境,框架启动以后调用parentRuner的run方法 ,其中>本质是Runner.run()表示了整个执行入口 ,接着通过Runner执行器中的classBlock方法>创建出statement对象,statement对象封装的evaluate()方法中执行以Test注解修饰的方法集合 ,然后对通过获取BeforeClass注解的方法集合、AfterClass注解的方法集合、statement对象进行排序最终返回一个statment对象,并调用statement的evaluate方法启动执行case的一系列动作也就是statement的链表
具体源码实现分析
源码执行case流程概览
上图,假设存在8条用例,其中包含2条以@BeforeClass修饰的用例、3条以@AfterClass修饰的用例、3条以@Test修饰的用例,具体执行流程如上图;
源码解读
首先程序执行入口run()方法,决定了整个测试的执行流程;
【junit封装的,BlockJUnit4ClassRunner类(执行器的一种)作为JUnit4的默认Runner实现 继承了 ParentRunner类;
ParentRunner类 继承了 Runner类,重写了 Runner类中的run()方法,作为程序执行入口】
注释:一般常见会在junit单测类上出现@RunWith(XXXX.class)的注解
它是junit框架支持的一个类级别的注释;参数是一个执行器,是Runner类的子类,是用来指定运行测试用例的工具。使用@RunWith()注解为这个类指定一个特定的Runner。当我们没有指定@RunWith()的时候,会自动使用Junit的默认Runner——BlockJunit4ClassRunner。
常用的Runner: Suite:测试套件 、Category:按种类区分的套件、Parameterized:参数化测试、
Theories:排列组合。 例如:@RunWith(Suite.class)。
/**
* Run the tests for this runner.
*
* @param notifier will be notified of events while tests are being run--tests being
* started, finishing, and failing
*/
public abstract void run(RunNotifier notifier);
@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);
} catch (StoppedByUserException e) {
throw e;
} catch (Throwable e) {
testNotifier.addFailure(e);
}
}
相关调用方法:
/**
*classBlock()负责构造这样一个Statement:
*1、通过注解校验和过滤筛选出需要执行的测试方法
*2、对这些测试方法使用装饰器模式进行 @BeforeClass 和 @AfterClass和@ClassRule的包装
*/
/**
* Constructs a {@code Statement} to run all of the tests in the test class.
* Override to add pre-/post-processing. Here is an outline of the
* implementation:
* <ol>
* <li>Determine the children to be run using {@link #getChildren()}
* (subject to any imposed filter and sort).</li>
* <li>If there are any children remaining after filtering and ignoring,
* construct a statement that will:
* <ol>
* <li>Apply all {@code ClassRule}s on the test-class and superclasses.</li>
* <li>Run all non-overridden {@code @BeforeClass} methods on the test-class
* and superclasses; if any throws an Exception, stop execution and pass the
* exception on.</li>
* <li>Run all remaining tests on the test-class.</li>
* <li>Run all non-overridden {@code @AfterClass} methods on the test-class
* and superclasses: exceptions thrown by previous steps are combined, if
* necessary, wi