利用类反射和注解做自己的JUnit测试工具

来源:互联网 发布:java 堆栈pop报错 编辑:程序博客网 时间:2024/05/17 03:58
package cn.hncu.myJUnit;import java.lang.reflect.Method;import java.util.Scanner;public class MyJUnit {public static void main(String[] args) throws Exception {        System.out.println("请输入要被测试的类");        Scanner sc=new Scanner(System.in);        String className=sc.nextLine();        Class c=Class.forName(className);        Object obj=c.newInstance();                //获取该类中的所有方法        Method[] ms=c.getDeclaredMethods();        for(Method m:ms){        if(m.isAnnotationPresent(MyTest.class)){        System.out.println(m.getName()+"存在注解");        m.invoke(obj, null);        }        }}}



package cn.hncu.myJUnit;import java.lang.annotation.ElementType;import java.lang.annotation.Retention;import java.lang.annotation.RetentionPolicy;import java.lang.annotation.Target;@Target(value={ElementType.METHOD})@Retention(RetentionPolicy.RUNTIME)public @interface MyTest {}


0 0
原创粉丝点击