zoukankan      html  css  js  c++  java
  • 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!");
                }
            }
        }
    }
    


    参考资料:

  • 相关阅读:
    js 文件的操作
    js重点基础知识 以及小案例_最简单的轮播图 简单的动态表格( encodeURIComponent()编码比 encodeURI()编码)
    2阶——数据库连接池 c3p0 , druid, dbcp (其实所有的连接池都实现了dataSource接口,就可以调用getconnection方法)
    2阶——JDBC,JDBCTemplate(操作数据库)
    vue + django 批量删除
    简单的模糊搜索 Vue + django
    vue 父子组件传参简单的分页
    vue 多对多反序列化上传图片
    模型里的 抽象类 表继承
    django 多对多反序列添加
  • 原文地址:https://www.cnblogs.com/linkworld/p/9059227.html
Copyright © 2011-2022 走看看