JUnit4.12 源码分析之TestClass
1. TestClass
// 源码:org.junit.runners.model.TestClass
// 该方法主要提供方法校验和注解搜索
public class TestClass implements Annotatable{
private static final FieldComparator FIELD_COMPARATOR = new FieldComparator();
private static final MethodComparator METHOD_COMPARATOR = new MethodComparator();
// 三个成员变量
private final Class<?> clazz;
private final Map<Class<? extends Annotation>, List<FrameworkMethod>> methodsForAnnotations;
private final Map<Class<? extends Annotation>, List<FrameworkField>> fieldsForAnnotations;
// 由于JDK对标注的处理代价高昂,此处,构造器的作用是为了保证实例的共用
public TestClass(Class<?> clazz){
this.clazz = clazz;
if(clazz != null && clazz.getConstructors().length > 1){
throw new IllegalArgumentException(
"Test class can only have one constructor");
}
Map<Class<? extends Annotation>, List<FrameworkMethod>> methodsForAnnotations =
new LinkedHashMap<Class<? extends Annotation>, List<FrameworkMehod>>();
Map<Class<? extends Annotation>, List<FrameworkField>> fieldsForAnnotations =
new LinkedHashMap<Class<? extends Annotation>, List<FrameworkField>>();
scanAnnotatedMembers(methodsForAnnotations, fieldsForAnnotations);
this.methodsForAnnotations = makeDeeplyUnmodifiable(methodsForAnnotations);
this.fieldsForAnnotations = makeDeeplyUnmodifiable(fieldsForAnnotations);
}
...(略)
}
// 测试代码
public class TestUnit(){
public TestUnit(){}
@Test
public void addx(int x){
assertEquals(5, 1+x);
}
@Test
public void add(){
assertEquals(5.0, 4.0, 0.1);
}
@Test
public void hello(){
assertEquals(5.0, 4.0, 0.1);
}
}
public class TestClassDemo{
public static void main(String[] args) throws Throwable{
TestClass kclass = new TestClass(TestUnit.class);
System.out.println(kclass.getName());
// Test.class 就是类中标明 @Test 注解的方法
List<FrameworkMethod> list = kclass.getAnnotatedMethods(Test.class);
for(FrameworkMethod fm : list){
try{
fm.invokeExplosively((TestUnit)kclass.getJavaClass().newInstance(), new Object[0]);
}catch(Throwable e){
System.out.println(e);
}finally{
System.out.println(fm.getName() + " invoked!");
}
}
}
}

参考资料:
JUnit4.12 源码分析之TestClass的更多相关文章
- JUnit4.12 源码分析之Statement
1. Statement 抽象类Statement作为命令模式的Command,只有一个方法 各种Runner作为命令模式中的Invoker,将发出各种Statement,来表示它们运行JUnit测试 ...
- JUnit4.12 源码分析(二)之TestRule
1. TestRule TestRule和@Before,@After,@BeforeClass,@AfterClass功能类似,但是更加强大; JUnit 识别TestRule的两种方式: 方法级别 ...
- 【JUnit4.10源码分析】5 Statement
假设要评选JUnit中最最重要的类型.或者说核心,无疑是org.junit.runners.model.Statement.Runner等类型看起来热闹而已. package org.junit.ru ...
- 【JUnit4.10源码分析】6.1 排序和过滤
abstract class ParentRunner<T> extends Runner implements Filterable,Sortable 本节介绍排序和过滤. (尽管JUn ...
- 12.源码分析—如何为SOFARPC写一个序列化?
SOFARPC源码解析系列: 1. 源码分析---SOFARPC可扩展的机制SPI 2. 源码分析---SOFARPC客户端服务引用 3. 源码分析---SOFARPC客户端服务调用 4. 源码分析- ...
- 【JUnit4.10源码分析】3.4 Description与測试树
Description使用组合模式描写叙述一个測试树.组合模式中全部元素都是Composite对象. Description有成员变量private final ArrayList<Descri ...
- 【JUnit4.10源码分析】5.2 Rule
标注@Rule TestRule是一个工厂方法模式中的Creator角色--声明工厂方法. package org.junit.rules; import org.junit.runner.Descr ...
- JUnit4.8.2源码分析-1 说明
阅读本系列文章时须要知道的: JUnit是由GOF 之中的一个的Erich Gamma和 Kent Beck 编写的一个开源的单元測试框架,分析JUnit源码的主要目的是学习当中对设计模式的运用.JU ...
- Solr4.8.0源码分析(12)之Lucene的索引文件(5)
Solr4.8.0源码分析(12)之Lucene的索引文件(5) 1. 存储域数据文件(.fdt和.fdx) Solr4.8.0里面使用的fdt和fdx的格式是lucene4.1的.为了提升压缩比,S ...
随机推荐
- FreeRtos——任务删除,改变任务优先级
以下转载自安富莱电子: http://forum.armfly.com/forum.php vTaskDelete() API 函数任务可以使用 API 函数 vTaskDelete()删除自己或其它 ...
- win7如何连接蓝牙键盘
控制面板->硬件和声音->查看设备和打印机->添加设备
- love2d--glsl01简单的渲染
love2d一个好玩的地方是支持glsl1.2,并修改简化,glsl1.2的文档 地址为:http://www.opengl.org/registry/doc/GLSLangSpec.Full.1.2 ...
- java ssm框架入门(二)添加语言滤器
使用过滤器是在web.xml中使用filter,以下是码过滤器,过滤所有资源的使用 web.xml <filter> <filter-name>setCharactor< ...
- MyBatis Geneator详解<转>
MyBatis Geneator中文文档地址: http://generator.sturgeon.mopaas.com/ 该中文文档由于尽可能和原文内容一致,所以有些地方如果不熟悉,看中文版的文档的 ...
- linux 域和xenomai 实时域之间的交互
/* * XDDP-based RT/NRT threads communication demo. * * Real-time Xenomai threads and regular Linux t ...
- 获取checkbox 组成字符串
<input type="checkbox" id="goods_server_name" name="goods_server_name[]& ...
- 001Maven_基本介绍及安装
Maven入门教程 介绍.环境配置 Maven介绍 Maven是一个采用纯Java编写的开源项目管理工具, Maven采用了一种被称之为Project Object Model (POM)概念来管理项 ...
- 什么是Spring Cloud
Spring Cloud是一系列框架的有序集合.它利用Spring Boot的开发便利性巧妙地简化了分布式系统基础设施的开发,如服务发现注册.配置中心.消息总线.负载均衡.断路器.数据监控等,都可以用 ...
- 【BZOJ】1670: [Usaco2006 Oct]Building the Moat护城河的挖掘(凸包)
http://www.lydsy.com/JudgeOnline/problem.php?id=1670 裸打了凸包.. #include <cstdio> #include <cs ...