Java注解示例-自定义Junit框架

https://zhuanlan.zhihu.com/p/60966151

别忘了设置保留策略为RetentionPolicy.RUNTIME。

代码结构

image.png

MyBefore注解

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface MyBefore {
}

MyTest注解

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface MyTest {
}

MyAfter注解

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface MyAfter {
}

EmployeeDAOTest

//EmployeeDAO的测试类
public class EmployeeDAOTest {
    @MyBefore
    public void init() {
        System.out.println("初始化...");
    }

    @MyAfter
    public void destroy() {
        System.out.println("销毁...");
    }

    @MyTest
    public void testSave() {
        System.out.println("save...");
    }

    @MyTest
    public void testDelete() {
        System.out.println("delete...");
    }
}

MyJunitFrameWork

public class MyJunitFrameWork {
    public static void main(String[] args) throws Exception {
        // 1.先找到测试类的字节码:EmployeeDAOTest
        Class clazz = EmployeeDAOTest.class;
        Object obj = clazz.newInstance();
        // 2.获取EmployeeDAOTest类中所有的公共方法
        Method[] methods = clazz.getMethods();
        /* 3.迭代出每一个Method对象
                     判断哪些方法上使用了@MyBefore/@MyAfter/@MyTest注解
                */
        List<Method> mybeforeList = new ArrayList<>();
        List<Method> myAfterList = new ArrayList<>();
        List<Method> myTestList = new ArrayList<>();
        for (Method method : methods) {
            if(method.isAnnotationPresent(MyBefore.class)){
                //存储使用了@MyBefore注解的方法对象
                mybeforeList.add(method);
            }else if(method.isAnnotationPresent(MyTest.class)){
                //存储使用了@MyTest注解的方法对象
                myTestList.add(method);
            }else if(method.isAnnotationPresent(MyAfter.class)){
                //存储使用了@MyAfter注解的方法对象
                myAfterList.add(method);
            }
        }

        // 执行方法测试
        for (Method testMethod : myTestList) {
            // 先执行@MyBefore的方法
            for (Method beforeMethod : mybeforeList) {
                beforeMethod.invoke(obj);
            }
            // 测试方法
            testMethod.invoke(obj);
            // 最后执行@MyAfter的方法
            for (Method afterMethod : myAfterList) {
                afterMethod.invoke(obj);
            }
        }
    }
}

执行结果:

image.png

标签: JAVA, 注解

评论已关闭