自己写的测试框架(注解版本和配置文件版本)

【注解版本】的测试框架
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();
	}
}
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值