请编写程序,模拟JUnit的@Test注解

请编写程序,模拟JUnit的@Test注解

public class Demo {
    
    
    @MyDemo
    public void mentod1() {
    
    
        System.out.println("1");
    }

    public void mentod2() {
    
    
        System.out.println(2);
    }

    public void mentod3() {
    
    
        System.out.println(3);
    }

    @MyDemo
    public void mentod4() {
    
    
        System.out.println("4");
    }


    public static void main(String[] arge) throws Exception {
    
    
        Demo demo = new Demo();
        Class clas = Demo.class;
        Method[] declaredMethods = clas.getDeclaredMethods();
        for (Method declaredMethod : declaredMethods) {
    
    
            if (declaredMethod.isAnnotationPresent(MyDemo.class)){
    
    
                declaredMethod.invoke(demo);
            }
        }
    }

}


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

猜你喜欢

转载自blog.csdn.net/weixin_52067329/article/details/115448420