JUnit3原型

实现过程比较简单,就是执行指定类中以Test开头的公有无参方法,不做阐述了~


实现代码:

public class MyJUnit3 {
	/**
	 * 传入类全名,执行其中以Test开头的公有无参方法
	 * @param className
	 * @throws Exception
	 */
	public static void RunTest(String className) throws Exception{
		Class clazz = Class.forName(className);
		for(Method m : clazz.getDeclaredMethods()){
			if(m.getName().startsWith("Test")){
				m.invoke(clazz.newInstance()); //执行方法,必须传入匹配的对象,否则抛出异常
			}
		}
	}
}

测试代码:

public class Tests {
//非Test开头
public static void sayHello(){
System.out.println("Hello!!!");
}

public static void TestsayHello(){
System.out.println("Hello!I'm test!");
}

public void Test(){
System.out.println("I'm test~~~");
}
//私有方法
private  void TestHello(){
System.out.println("Hello");
}

public static void main(String[] args) throws Exception {
//执行一个类中以Test开头的方法
MyJUnit3.RunTest("com.yuer.Tests");
}
}

猜你喜欢

转载自blog.csdn.net/chw0629/article/details/80298224