阅读本系列文章时须要知道的:

JUnit是由GOF 之中的一个的Erich Gamma和 Kent Beck 编写的一个开源的单元測试框架,分析JUnit源码的主要目的是学习当中对设计模式的运用。JUnit也是一个研究怎样应对版本号升级和接口变化的案例。

链接1:源码分析

JUnit4.8.2源码分析-1单元測试类

JUnit4.8.2源码分析-2 Request和Description

JUnit4.8.2源码分析-3 TestClass 和RunnerBuilder

JUnit4.8.2源码分析-4 RunNotifier与RunListener

JUnit4.8.2源码分析-1说明

JUnit4.8.2源码分析-1说明

JUnit4.8.2源码分析-1说明

JUnit4.8.2源码分析-1说明

链接2:使用JUnit的样例

1.JUnit4.8.2源码问题

因为yqj2065下载和使用的BlueJ所集成的版本号是JUnit4.8.2。所以就分析一下JUnit4.8.2的源码(CSDN下载)。解压后将它们导入BlueJ;编译它们时BlueJ会警告编译的类已经在BlueJ的库中,编译后使用的还将是库中的类,于是熟悉了的类我们能够在BlueJ中删除(反正我也不准备打包替代库中的JUnit)。

某些JUnit类型,看完了并且不准备回头再看时。yqj2065会在BlueJ中将它删除——每删除一个表示自己又前进了一点。删除如NullBuilder时,将import
org.junit.internal.builders.NullBuilder加到本包的它的客户类中(其它包使用的。是BlueJ库中引入的包文件里的类)。以保证整个项目能够编译和生成JavaDoc。

在本系列文章中。大多数情况我不会把JUnit4.8.2源码粘贴出来,读者应该有自己的拷贝,或者看这里http://www.docjar.com/projects/JUnit-4.7-code.html

2.熟悉JUnit的使用吗?

阅读源码。必须知道该框架的设计需求。假设精通JUnit。单元測试的需求应该较熟悉。大多数人如我。仅仅是简单地使用JUnit。

所以。有一些怎样使用JUnit的内容须要学习。

这里先从简单的样例入手。说明myTest包中程序的组织。

①应用程序/业务类(待測试的目标类)HelloWorld。能够在HelloWorld的类体中用main直接測试。TestHelloWorld演示了直接測试和模拟JUnit4的基本步骤的測试,见Java Annotation 提要

②为了使用JUnit4測试它,须要设计一个单元測试类HelloWorldTest。当然。单元測试类在IDE如BlueJ中。我们不须要写程序。

package myTest;//myTest.units
public class HelloWorld {
public double add(double m,double n){
return m+n;
}
public double add2(double m,double n){
return m+n;
}
}
package myTest;
import org.junit.Test;//@Test
import static org.junit.Assert.*;//assertEquals
public class Unit0{
@Test
public void add(){
HelloWorld h = new HelloWorld();
assertEquals(7.0, h.add(1, 2), 0.1);
}
}

单元測试类TestInJUnit4则是手工敲的代码,单元測试类图标为暗绿色,能够直接运行其@Test方法。

JUnit将处理的是单元測试类。@Test等标注/Annotation定义一项測试。

JUnit通过反射解析RUNTIME标注

单元測试类的一个測试是一个public void 方法

③为了验证JUnit4.8.2源码,我们能够直接编写XxxUnit单元測试类(包括各种标注)。而验证代码通常取名<JUnit class name>Demo,如RequestDemo。

3.单元測试类

编写单元測试类时,最经常使用的是各种标注、org.junit.Assert、Assume;须要提供很多其它代码的測试,请參考:

JUnit4.8.2的标注列举例如以下。

@Test标注的源码

package org.junit;
import java.lang.annotation.*; @Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD})
public @interface Test {
/**
* Default empty exception
*/
static class None extends Throwable {
private static final long serialVersionUID= 1L;
private None() {
}
} /**
* Optionally specify <code>expected</code>, a Throwable, to cause a test method to succeed iff
* an exception of the specified class is thrown by the method.
*/
Class<? extends Throwable> expected() default None.class; /**
* Optionally specify <code>timeout</code> in milliseconds to cause a test method to fail if it
* takes longer than that number of milliseconds.*/
long timeout() default 0L;
}

org.junit.Ignore @Target({ElementType.METHOD, ElementType.TYPE})

@Before和@After标示的方法仅仅能各有一个。代替了JUnit曾经版本号中的setUp和tearDown方法

org.junit.BeforeClass @Target(ElementType.METHOD)

org.junit.Before @Target(ElementType.METHOD)

org.junit.AfterClass @Target(ElementType.METHOD)

org.junit.After @Target(ElementType.METHOD)

org.junit.Rule

org.junit.runner.RunWith @Target(ElementType.TYPE),使用指定Runner执行測试。默认的Runner为org.junit.runners.JUnit4。

org.junit.runners.Suite.SuiteClasses @Target(ElementType.TYPE)。将全部须要执行的測试类组成组/ Suite。一次性的执行以方便測试工作。

org.junit.runners.Parameterized.Parameters @Target(ElementType.METHOD),參数化測试

org.junit.experimental.theories.suppliers. TestedOn

org.junit.experimental.theories. DataPoint

org.junit.experimental.theories.DataPoints

org.junit.experimental.theories.ParametersSuppliedBy

org.junit.experimental.theories.Theory

org.junit.experimental.categories.Categories.ExcludeCategory

org.junit.experimental.categories.Categories.IncludeCategory

org.junit.experimental.categories.Category

JUnit4.8.2源码分析-1 说明的更多相关文章

  1. JUnit4.8.2源码分析-4 RunNotifier与RunListener

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

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

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

  3. JUnit4.12 源码分析之TestClass

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

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

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

  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. 为用户分配角色 C#

    开发网站时,在后台管理系统中,如果有多类角色,将会涉及到为角色分配用户的功能,或者是为用户选择角色.为用户分配角色相对来说操作的数据量比较小,因为系统所设定的角色不会有很多种.而如果是为角色分配用户, ...

  2. hdu 2215 & hdu 3932(最小覆盖圆)

    Maple trees Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

  3. Interface与abstract类的区别

    含有abstract修饰符的class即为抽象类,abstract 类不能创建的实例对象.含有abstract方法的类必须定义为abstract class,abstract class类中的方法不必 ...

  4. swagger (九)

    创建创建microservicecloud-swagger pom文件 eureka: client: #客户端注册进eureka服务列表内 service-url: defaultZone: htt ...

  5. 取消Gridvie中button的焦点

    Gridview中添加button,onclick方法使得 GridView的setOnItemClickListener方法无效. 解决方法: 设置Button的XML布局文件,也就是自定义的Ada ...

  6. [Codeforces 7E] Defining Macros

    Link:http://codeforces.com/problemset/problem/7/E Brief Introduction:一个表达式由多个“Macros”组成,每个Macro都为一个整 ...

  7. 【权值分块】bzoj1208 [HNOI2004]宠物收养所

    不多说.比pb_ds还是要快不少的. #include<cstdio> #include<algorithm> #include<cmath> using name ...

  8. 5.6(java学习笔记) queue

    一.queue接口 queue的中文意思是队列,是一种以先进先出方式处理数据的集合. 队列还提供额外的插入.提取和检查操作.这些方法都以两种形式存在:一种在操作失败时抛出异常,另一种返回特殊值(根据操 ...

  9. 建立Spring项目的基础

    1.新建web项目 2.在lib下添加这五个包 3.新建applicationContext.xml(一定在src目录下)

  10. C#调用页面中的窗体中的方法,获取窗体的元素。

    页面中的窗体 <div class="div_width" style="width: 100%; height: 95%;"> <ifram ...