【注解版本】的测试框架
Check注解(用于标识要测试的代码)
package demo70;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface Check {
}
Pro注解(用于加载全限定类名)
package demo70;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface Pro {
String className();
//String methodName();
}
Check注解的解析程序(测试框架源码)
package demo70;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
// 做出两种改版:一个是属性集,一个是注解方式(都是通过配置文件的方式加载测试文件)
@Pro(className="demo70.Calculator")
public class TestCheck {
public static void main(String[] args) throws Exception {
// // 1.创建计算器类对象
// Calculator c = new Calculator();
// // 2.获取计算器类对象
// Class cls = c.getClass();
Class testCheckClass = TestCheck.class;
// 此处有一个底层步骤是实现了Pro注解类型的接口
// 并且重写了Pro内部的属性
Pro an = (Pro) testCheckClass.getAnnotation(Pro.class);
// 调用内部属性
String className = an.className();
// 创建类对象
Class cls = Class.forName(className);
// 创建类的实例
Object c = cls.newInstance();
// 3.获取所有方法
Method[] methods = cls.getMethods();
int number = 0; // 出现异常的次数
BufferedWriter bw = new BufferedWriter(new FileWriter("bug.txt"));
for (Method method : methods) {
// 4.判断方法上是否有Check注解
if (method.isAnnotationPresent(Check.class)) {
// 5.有,执行
try {
method.invoke(c);
} catch (Exception e) {
// 6.捕获异常
// 记录到文件中
number++;
bw.write(method.getName() + "方法出异常了");
bw.newLine();
bw.write("异常的名称:" + e.getCause().getClass().getSimpleName());
bw.newLine();
bw.write("异常的原因:" + e.getCause().getMessage());
bw.newLine();
bw.write("-----------------------------------");
bw.newLine();
}
}
}
bw.write("本次测试一共出现"+ number +"次异常");
bw.flush();
bw.close();
}
}
============================================================
【配置文件版本】的测试框架
Check注解(用于标识要测试的代码)
package demo70;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface Check {
}
Check注解的解析程序(测试框架源码)
package demo70;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.util.Properties;
// 做出两种改版:一个是属性集,一个是注解方式(都是通过配置文件的方式加载测试文件)
public class TestCheck2 {
public static void main(String[] args) throws Exception {
// // 1.创建计算器类对象
// Calculator c = new Calculator();
// // 2.获取计算器类对象
// Class cls = c.getClass();
// 1.创建属性集
Properties pro = new Properties();
// 使用类加载器读取文件,转换为流对象
ClassLoader classLoader = TestCheck2.class.getClassLoader();
InputStream is = classLoader.getResourceAsStream("pro.properties");
// 使用属性集中的load方法读取流
pro.load(is);
// 使用属性集中的方法获取配置文件的参数
String className = pro.getProperty("className");
// 创建类对象
Class cls = Class.forName(className);
// 创建类的实例
Object c = cls.newInstance();
// 3.获取所有方法
Method[] methods = cls.getMethods();
int number = 0; // 出现异常的次数
BufferedWriter bw = new BufferedWriter(new FileWriter("bug.txt"));
for (Method method : methods) {
// 4.判断方法上是否有Check注解
if (method.isAnnotationPresent(Check.class)) {
// 5.有,执行
try {
method.invoke(c);
} catch (Exception e) {
// 6.捕获异常
// 记录到文件中
number++;
bw.write(method.getName() + "方法出异常了");
bw.newLine();
bw.write("异常的名称:" + e.getCause().getClass().getSimpleName());
bw.newLine();
bw.write("异常的原因:" + e.getCause().getMessage());
bw.newLine();
bw.write("-----------------------------------");
bw.newLine();
}
}
}
bw.write("本次测试一共出现"+ number +"次异常");
bw.flush();
bw.close();
}
}