Case: integrated case notes (analog Junit test @Test)

Demand for integrated case notes:

      Analog Junit test @Test achieve the following two cases
      if the annotated method @Test added, you can use unit tests to run
      if there is no added @Test annotation offense, run unit tests can not be used

analysis:

  1. A custom annotation @MyTest, adding metadata annotations to the annotation (can only be used in the method, the effective run)
  2. Create a test class, define multiple methods in the test class, so use a custom annotations on the part of the method @MyTest
  3. A reflective technology acquisition test class file object class
  4. Use class file object newInstance instantiating the object
  5. Reflection technique using all the acquired test class method returns an array of Method
  6. Method traverse the array, each acquisition method Method
  7. Is there a specified annotation @MyTest the method of determining the acquired Method
  8. If the method has @MyTest comments, let the method run

Code:

1, a custom annotations @MyTest, adding metadata annotations to the annotation (can only be used in the method, the effective run)

package com.ccc.demo09Annotation;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

//1、自定义一个注解@MyTest,给注解添加元注解(只能在方法上使用,运行有效)
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface MyTest {
}

2, create a test class, define multiple methods in the test class, so use a custom annotations on the part of the method @MyTest

package com.ccc.demo09Annotation;

//2.创建一个测试类,在测试类中定义多个方法,让部分方法上使用自定义的注解@MyTest
public class DemoMyTest {
    public void test01(){
        System.out.println("test01方法");
    }
    
    @MyTest
    public void test02(){
        System.out.println("test02方法");
    }
    
    public void test03(){
        System.out.println("test03方法");
    }
    
    @MyTest
    public void test04(){
        System.out.println("test04方法");
    }
}

The remaining six steps

package com.ccc.demo09Annotation;

import java.lang.reflect.Method;

public class Demo01AnnotationTest {
    public static void main(String[] args) throws Exception {
        
        //3.使用反射技术获取测试类的class文件对象
        Class clazz = Class.forName("com.ccc.demo09Annotation.DemoMyTest");
        
        //4.使用class文件对象中的方法newInstance实例化对象
        Object obj = clazz.newInstance();
        
        //5.使用反射技术获取到测试类中所有方法,返回一个Method数组
        Method[] declaredMethods = clazz.getDeclaredMethods();
        
        //6.遍历Method数组,获取每一个Method方法
        for (Method method : declaredMethods) {
            
            //7.判断获取到的Method方法上是否有指定的注解@MyTest
            if(method.isAnnotationPresent(MyTest.class)){
                
                //8.如果方法有@MyTest注解,让方法运行
                method.invoke(obj);
            }
        }
    }
}

Guess you like

Origin blog.csdn.net/qq_45083975/article/details/91985716