junit-反射-注解

junit测试

黑盒测试:不写代码
白盒测试:junit测试

1.测试类名:****Test
包名称:cn.itcast.test

2.方法名:testAdd();
返回值:void
参数列表:空参

3.给方法加@Test
4.导入依赖

红色:失败
绿色:成功

断言

public class Demo {
    public int add(int a,int b){
        return a+b;
    }
}
import org.junit.Assert;
import org.junit.Test;

public class DemoTest {
  @Before//永远先运行
    public void init(){
        System.out.println("kaoshi...");
    }
    @After//永远最后运行
    public void  close(){
        System.out.println("jieshule");

    }
    @Test
    public void testAdd(){
        Demo demo = new Demo();
        int re = demo.add(1, 2);
        //断言
        Assert.assertEquals(3,re);
    }
}

反射

框架设计的灵魂

  • 框架:半成品软件
  • 反射:将类组成部分封装为其他对象,反射机制
    1.运行过程中 ,可操作对象
    2.可解耦
    3种方式:
    Class.forName(“全类名”) 返回class对象:配置文件
    类名.Class:参数传递
    对象.getClass:对象获取
    同一个字节码文件只会被加载一次

使用Class对象

  • 获取功能
  1. getFields,获取public修饰成员变量,用来设置值,获取值get
  2. getDeclareFiled;获取所有成员变量
    obj.setAccessible(true);暴力反射,才可get
  3. getConstruction获取构造方法
    创建对象newInstance,也可class.newInstance
  4. getMethod获取指定名称的方法 invoke执行方法

写个框架,不改代码,创建任意类对象,可以执行任意方法

import java.io.InputStream;
import java.lang.reflect.Method;
import java.util.Properties;

public class DemoTest {
    public static void main(String[] args) throws Exception {
        //加载文件转为集合
        Properties pro=new Properties();
        ClassLoader classLoader = DemoTest.class.getClassLoader();
        InputStream is = classLoader.getResourceAsStream("pro.properties");
        pro.load(is);
        //获取配置文件中数据
        String className = pro.getProperty("className");
        System.out.println(className);
        String methodName = pro.getProperty("methodName");
         //加载进内存
        Class cls = Class.forName(className);
        //创建对象
        Object obj = cls.newInstance();
        //获取方法
        Method method = cls.getMethod(methodName);
        //执行方法
        method.invoke(obj);


    }
}

src 配置文件

className=cn.itcast.day04.demo10.Demo//反射机制
methodName=eat

注解

给计算机看
给程序猿看:注释
1.5之后

  • 编译检查;
    @注解名称
  • 编写文档
    javadoc
  • 代码分析(测试)

预注解
@Override:检测是否继承父类
@Deprecated:过时了
@SuppressWarnings:压制警告,一般传递all

自定义注解
本质是一个接口
格式:
元注解+

public @interface Myao{
public abstract String name() default "zhangsan";
}

1.抽象方法的返回值:基本,String,枚举,注解,以上类型的数组
2.使用时要赋值,除非默认,数组要用{}赋值
3.只有value,直接定义值

元注解
@Target 作用的位置ElementType Type(类) MEthod(方法) FIELD(成员变量)
@Retention 描述注解被保留的阶段 Runtime
@Documented:描述注解是否被抽取到API文档中
@Inherited 描述是否被子类继承

@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
public @interface Pro {
    String className();
    String methodName();
}
import java.lang.reflect.Method;

@Pro(className = "cn.itcast.day04.demo10.Demo",methodName = "eat")
public class Demo {
    public static void main(String[] args) throws Exception{
        //解析注解
        //获取字节码
        Class<Demo> demoClass = Demo.class;
        //获取注解对象
        Pro an = demoClass.getAnnotation(Pro.class);
        //调用抽象方法
        
        
        String s = an.className();
        String s1 = an.methodName();
        
        //后面同反射
        System.out.println(s);
        Class cls = Class.forName(s);
        Object o = cls.newInstance();
        Method method = cls.getMethod(s1);
        method.invoke(o);


    }
}

大多数使用注解,不是自定义;
注解像是个标签;

发布了41 篇原创文章 · 获赞 13 · 访问量 5889

猜你喜欢

转载自blog.csdn.net/qq_41344974/article/details/105228330
今日推荐