Custom @MyTest annotation, handwritten @Test unit test

What do you know about annotations? Have you tried @Test annotation?


purpose:

Complete the custom annotation @MyTest, and implement the method marked with the annotation and start it! (Simulate @Test annotation for unit testing)

step:

  1. Create a new annotation class (annotation) , named MyTest
  2. Create a TestJunit unit test class and write a few methods, such as:public void test1()
  3. Create a MyTestDemo test class (main function implementation class), this class mainly uses the reflection mechanism to achieve the start of the TestJunit unit test class with @MyTest annotated method
  4. The life cycle of the annotation class is consistent with the reflection mechanism , that is, the defined annotation can be retained until runtime, and the annotation information can be obtained through the reflection mechanism
  5. Write the MyTestDemo test class, use the reflection to get the Class object of the TestJunit unit test class, and get all the method objects in the unit test class, traverse all the method objects, as long as the method annotated with @MyTest is executed, it is not annotated. Give any processing operations
  6. Start the test class and view the results (execution results, at the end!)

note:

  1. In the custom annotation class, no annotation body is written, that is, no default value is given. Because the annotation only serves as an identification, the identification needs to be started
  2. The annotation class is also a .class file after compilation
  3. To complete the custom annotation operation through the reflection mechanism, you must give the same life cycle as annotation and reflection
  4. You need to know that we can't complete the plug-in functions like Junit4 and Junit5, we can selectively execute the annotated method, and we don't admit that we have the strength to write the plug-in IDEA. Won't give you run method startup items
  5. Whether you are a new reader or an old reader, no matter you read my article, you must pay attention to the comment information in my code, otherwise you will lose a lot of knowledge, thank you!

Post code:

MyTest annotation class

package com.mylifes1110.java.anno;

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

/**
 * 此自定义注解@MyTest只是作为需要单元测试的标记,不需要做默认值
 * @Retention注解表示给与@MyTest注解生命周期
 * 当前定义的注解可以保留到运行时,通过反射机制可以获取注解信息
 * 否则反射将对注解没有任何作用,失去了该意义和自定义单元测试的初衷
 */
@Retention(RetentionPolicy.RUNTIME)
public @interface MyTest {
//    String value() default "";
}

TestJunit unit test class

package com.mylifes1110.java.test.junit;

import com.mylifes1110.java.anno.MyTest;
import org.junit.Test;

public class TestJunit {

    @Test
    public void test1() {
        System.out.println("---test1---");
    }

    @MyTest
    public void test2() {
        System.out.println("---test2---");
    }

    @MyTest
    public void test3() {
        System.out.println("---test3---");
    }

    public void test4() {
        System.out.println("---test4---");
    }
}

MyTestDemo test class (mainly function and test)

package com.mylifes1110.java.test.junit;

import com.mylifes1110.java.anno.MyTest;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 * 让自定义的MyTest注解起作用
 * 通过反射,扫描TestJunit类中有哪些方法上方加了MyTest注解
 * 如果加@MyTest注解的则执行它
 * 如果没有加@MyTest注解的则不做任何处理
 */
public class MyTestDemo {
    public static void main(String[] args) {
        /**
         * 1.获取TestJunit类对应的Class对象
         */
        Class<TestJunit> junitClass = TestJunit.class;
        /**
         * 获取TestJunit类中所有的方法对象
         */
        Method[] methods = junitClass.getMethods();

        /**
         * 遍历所有方法对象查找有与没有@MyTest注解,并做出响应处理
         */
        for (Method method : methods) {
            boolean present = method.isAnnotationPresent(MyTest.class);
            /**
             * TestJunit类中有@MyTest注解的执行该方法
             */
            if (present) {
                try {
                    method.invoke(junitClass.newInstance());
                } catch (IllegalAccessException | InvocationTargetException | InstantiationException e) {
                    e.printStackTrace();
                }
            } else {
                /**
                 * TestJunit类中没有@MyTest注解的不做任何操作
                 * 此else分支冗余,只是为了做标记,让你们好理解
                 */
            }
        }
    }
}

Results of the:

Insert picture description here

Published 155 original articles · praised 337 · 90,000 views +

Guess you like

Origin blog.csdn.net/weixin_44170221/article/details/105556142