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 ...
随机推荐
- 服务器端cs文件
服务器端向mysql数据库写数据 using System; using Newtonsoft.Json; using Newtonsoft.Json.Linq; using System.Colle ...
- FreeRTOS 定时器组
以下转载自安富莱电子: http://forum.armfly.com/forum.php 本章节为大家讲解 FreeRTOS 支持的定时器组,或者叫软件定时器,又或者叫用户定时器均可.软件定时器的功 ...
- CentOs下安装gcc/g++/gdb
使用yum安装gcc:yum install gcc即可.使用:which gcc 查看是否安装成功 使用yum安装g++:yum install gcc-c++ 即可.使用:which g++ 查看 ...
- 解决Error:Android Dex: com.android.dex.DexIndexOverflowException: Cannot merge new index 65918 into a
错误:Error:Android Dex: com.android.dex.DexIndexOverflowException: Cannot merge new index 65918 into a ...
- 安全 流程服务器开新机器 内外网 iptables 安全组 用户安全root用户的使用.
安全 流程服务器开新机器 内外网 iptables 安全组 用户安全root用户的使用.
- ]flexslider 中文文档 使用教程 参数手册
[原创]flexslider 中文文档 使用教程 参数手册 要改前人用的flexslider功能,但苦于找不到详细的文档教程,折磨了好久……(所以我才说不爱乱用插件) 为了福利下之后也苦于这个问题 ...
- (转)内核container_of(ptr,type,member) 解析
container_of(ptr,type,member) 用于在已知结构体里面成员member和该成员指针ptr(就是地址)和结构体类型type, 返回该成员所在的结构体的指针(就是地址), 例如 ...
- 书籍:Building Secure PHP Apps
Building Secure PHP Apps https://leanpub.com/buildingsecurephpapps
- mysql_ado的demo
winform程序 http://pan.baidu.com/s/1nvxm5br
- pychram最新注册码
2016年的激活码只能用到2017.2.25,于昨日已经过期了. 现提供最新激活码: BIG3CLIK6F-eyJsaWNlbnNlSWQiOiJCSUczQ0xJSzZGIiwibGljZW5zZW ...