当前位置 : 主页 > 网络安全 > 测试自动化 >

02-模拟Junit4功能

来源:互联网 收集:自由互联 发布时间:2021-06-19
package com.day2; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target;@Target({ElementType.METHOD})@Retention(RetentionPolicy.RUNTIM
package com.day2;

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

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

 

package com.day2;

public class User {
    
    @Test
    public void getName()
    {
        System.out.println("刘诗华测试");
    }
    
    
    @Test
    public void getTel()
    {
        System.out.println("15390725037");
    }
    
    
}

 

package com.day2;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;

public class Junit5 {
    
    public static void main(String[] args) throws Exception {
        
        Class clazz = Class.forName("com.day2.User");
        
        Object obj = clazz.newInstance();
        
        Method[] list = clazz.getDeclaredMethods();
        
        
        List<Method> arr=new ArrayList<>();
        for (Method m : list) {
            boolean b = m.isAnnotationPresent(Test.class);
            
            if(b)
            {
                arr.add(m);
            }
        }
        
        for (Method m : arr) {
            m.invoke(obj);
        }
        
        
    }
    
}
网友评论