简单 JUnit

package cn.itcast.myjunit;

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 MyTest {

}



package cn.itcast.myjunit;

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

public class MyJunit {

	/**
	 * @param args
	 * JUnit4 测试类执行方法:先获取类名,查找注解,反射执行测试类的方法
	 */
	public static void main(String[] args) {
		System.err.println("please input a class name:");
		Scanner sc = new Scanner(System.in);
		String clsName = sc.nextLine();
		
		try {
			Object o = Class.forName(clsName).newInstance();
			Method[] ms = o.getClass().getDeclaredMethods();
			
			/*
			 * for(Method m : ms)
			 */
			for(int i=0;i<ms.length;i++) {
				Method m = ms[i];
				if(m.isAnnotationPresent(MyTest.class)) {
					System.err.println("测试类"+o.getClass().getName()+"中的 "+m.getName()+"()方法 has annotation");
					m.invoke(o, null);
				}
			}
			
			System.err.println("done...............");
		} catch (InstantiationException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (IllegalArgumentException e) {
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			e.printStackTrace();
		}
	}

}

package cn.itcast.myjunit;

public class HelloJunit {
	
	@MyTest
	public void test() {
		System.err.println("my junit............");
	}
}

直接运行MyJunit, 根据提示输入:cn.itcast.myjunit.HelloJunit
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值