JUnit4.8.2源代码分析-1 说明
来源:互联网 发布:南风知我意温南叶小意 编辑:程序博客网 时间:2024/05/18 02:52
阅读本系列文章时需要知道的:
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.unitspublic 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;//@Testimport static org.junit.Assert.*;//assertEqualspublic 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 说明
- JUnit4.8.2源代码分析-5 Statement
- JUnit4.8.2源代码分析-3.2 Computer
- JUnit4.8.2源代码分析-2 org.junit.runner.Request
- JUnit4.8.2源代码分析-3 TestClass 和RunnerBuilder
- JUnit4.8.2源代码分析-4 RunNotifier与RunListener
- JUnit4.8.2源代码分析-3.1 Description-测试树
- JUnit4.8.2源代码分析-6.1 排序和过滤
- JUnit4.8.2源代码分析-5.1 Statement之复合命令
- 【JUnit4.10源代码分析】1.2 JUnit4定义的标注大全
- 【JUnit4.10源代码分析】0导航
- 【JUnit4.10源代码分析】5 Statement
- 【JUnit4.10源代码分析】5.2 Rule
- 【JUnit4.10源代码分析】6 Runner
- JUnit4-Description.java 源代码 解读与分析
- JUnit4-Result.java 源代码 解读与分析
- JUnit4说明
- 【JUnit4.10源代码分析】3.4 Description与测试树
- RDA5820收音机芯片驱动
- 记一次成功的安装MySQL5.0的经历
- 不能将参数 1 从const char [*]转换为LPCWSTR
- MySQL 5.6 root密码丢失,使用mysqld --skip-grant-tables出现错误提示
- ActiveMQ:设置多个并行的消费者
- JUnit4.8.2源代码分析-1 说明
- mysql导出数据库和恢复数据库代码
- PHP移动互联网开发笔记(6)——MySQL数据库基础回顾[1]
- Linux 批量解压tar包
- C++ 快到碗里来。
- OC内存管理
- 知、思、创
- AttributeError: 'Connection' object has no attribute 'connection_errors'
- 源码安装 OpenStack icehouse Trove 项目