JUnit4.8.2源码分析-1 说明

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

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



原文地址:https://www.cnblogs.com/yxysuanfa/p/7259219.html