---
title: "JUnit源码分析"
author: "white182517"
date: "2007-03-09T13:59:24Z"
tags: []
slug: "junit"
draft: false
lastmod: "2007-03-09T13:59:24Z"
id: 58600
url: "https://flytogether.iteye.com/blog/58600"
---

### 前言

JUnit框架通过几个接口就实现了一个灵活的测试框架，借研究Spring测试帮助类之机，重温一下。

### 主要接口&lt;o:p&gt; &lt;/o:p&gt;

##### Test

        描述测试的接口，核心业务方法run(TestResult result)方法用来运行测试，并收集测试结果。

##### TestCase

        一个TestCase可以用来定义多个测试，框架支持两种方式运行一个测试，静态方式下，可以通过重写runTest方法，定义需要激活的测试方法，示例如下： 

java 代码

1. TestCase test= new MathTest("add") {
2. public void runTest() {
3. testAdd();
4. }
5. };
6. test.run();

         

        动态方式使用反射实现runTest，缺省实现要求在构建TestCase时指定需要测试的方法名称，示例如下：

java 代码

1. TestCase test= new MathTest("testAdd");
2. test.run();

         

        其run(TestResult result)方法会调用TestResult的run(Test test)方法，而TestResult的run(Test test)方法在做一些结果收集处理后，反过来再调用TestCase的runBare方法来进行实际的测试方法调用。

      runBare是一种模板方法模式的实现，子类可以通过重写setUp,tearDown和runTest方法提供相应的测试逻辑。

##### TestSuite

        一个TestSuite可以包含一个或多个TestCase，这是组合模式的实现。可以使用动态方式定义TestSuite，示例如下： 

java 代码

1. TestSuite suite= new TestSuite();
2. suite.addTest(new MathTest("testAdd"));
3. suite.addTest(new MathTest("testDivideByZero"));

        也可以通过构造函数静态定义

java 代码

1. TestSuite suite= new TestSuite(MathTest.class);

        两种方式都是通过反射机制，解析传递进来的测试类，将方法前缀为将方法前缀为test的公有方法构建成测试实例，并添加到缓存的集合中，运行时再依次遍历这些测试实例完成测试。

        TestSuite的run(TestResult result)方法是运行测试的主要入口。

##### TestResult

        TestResult主要用来收集测试结果，内部使用两个集合区分测试失败和测试错误情况，并包含一个监听器列表。Test的run(TestResult result)方法会将控制权转交给TestResult，TestResult会捕获调用Test的runBare方法中的错误，并在测试启动和结束时通知监听器，如果对测试结果感兴趣，可以实现TestListener接口，这是监听器模式的实现。

##### TestListener

        测试监听器提供几个接口方法，用来对测试结果进行处理，如果对测试结果感兴趣，可以在调用Test的run(TestResult result)方法前，将测试监听器实例注册到TestResult实例。

&lt;o:p&gt; &lt;/o:p&gt;

### 运行序列图

下面我们通过序列图来看看这几个接口之间是如何交互的，这里以TextRunner为例：

![](http://www.iteye.com/topics/download/c7ed6943-084b-420b-90cc-8544bd297a0b)

1. TextRunner会从命令窗口中解析出需要测试的TestSuite实例，然后交给doRun方法处理。
2. doRun方法首先构建一个TestResult实例。
3. 然后调用TestSuite的run(TestResult result)方法。
4. run(TestResult result)询问TestResult的shouldStop方法判断调用者是否已经停止测试。
5. 如果TestResult的shouldStop返回false,将会遍历TestSuite的包含的每一个TestCase实例,重复执行如下步骤。
6. 调用TestCase的run(TestResult result)方法。
7. TestCase的run(TestResult result)委托给TestResult的run(Test test)方法。
8. TestResult的run(Test test)方法会调用监听器的startTest方法通知测试开始，然后调用TestCase的runBare执行真正的测试，最后调用监听器的endTest方法通知测试结束。&lt;o:p&gt; &lt;/o:p&gt;

### 扩展

        TestCase的runBare方法是实际执行测试的方法，该方法缺省构建一个模板方法实现。子类可以通过重写此方法提供自定义的实现，比如Spring的ConditionalTestCase就通过改写此方法提供条件化测试执行逻辑。

        JUnit自带的RepeatedTest类使用装饰器模式实现一个可重得执行的测试类扩展。&lt;o:p&gt; &lt;/o:p&gt;