基于注解一个简单的测试框架

该博客展示了如何使用Java注解进行方法级别的异常测试。通过创建一个`@Check`注解,标记可能抛出异常的方法,并在主方法中遍历并执行这些方法,捕获并记录异常信息到文件。在测试过程中,`Calculator`类的`Add`和`Div`方法由于除以零引发了`ArithmeticException`,异常信息被详细记录在文件中。
摘要由CSDN通过智能技术生成

首先准备一个被测试的类

package com.yunji.test02;/*
 @Author wanghongyuan
  @Date 2021/1/28 
 
*/

public class Calculator {
    @Check
    public void Add(){ int a = 1/0;System.out.println("1+0:"+(1+0)); }
    @Check
    public void Sub(){ System.out.println("1+0:"+(1-0));}
    @Check
    public void Div(){ System.out.println("1+0:"+(1/0));}
    @Check
    public void mul(){ System.out.println("1+0:"+(1*0));}
}

定义一个注解

package com.yunji.test02;/*
 @Author wanghongyuan
  @Date 2021/1/28 
 
*/

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

}

定义一个执行注解的主方法

package com.yunji.test02;/*
 @Author wanghongyuan
  @Date 2021/1/28 
 
*/

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class TestCheck {
    public static void main(String[] args) throws Exception {
        //创建一个需要被测试的对象。
        Calculator c = new Calculator();
        //获取字节码文件
        Class aClass = c.getClass();
        //获取所有方法
        Method[] methods = aClass.getMethods();
        int count = 0;
        BufferedWriter bw = new BufferedWriter(new FileWriter("C:\\Users\\why\\Desktop\\untitled\\src\\test\\java\\com\\yunji\\test02\\bug.txt"));
        for (Method method : methods) {
            //判断方法是否有我们标注的注解
            if (method.isAnnotationPresent(Check.class)){
                //有,我们执行方法
                try {
                    method.invoke(c);
                } catch (Exception e) {
                    //捕获异常
                    e.printStackTrace();
                    //记录到文件中
                    count++;
                    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("本次测试共出现"+count+"次异常");
        bw.flush();
        bw.close();
    }
}

最终执行结果

1+0:1
1+0:0
java.lang.reflect.InvocationTargetException
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:498)
	at com.yunji.test02.TestCheck.main(TestCheck.java:28)
Caused by: java.lang.ArithmeticException: / by zero
	at com.yunji.test02.Calculator.Add(Calculator.java:9)
	... 5 more
java.lang.reflect.InvocationTargetException
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:498)
	at com.yunji.test02.TestCheck.main(TestCheck.java:28)
Caused by: java.lang.ArithmeticException: / by zero
	at com.yunji.test02.Calculator.Div(Calculator.java:13)
	... 5 more

Process finished with exit code 0

文件记录的格式

Add方法出现异常
异常的名称是:ArithmeticException
异常的原因是:/ by zero
------------------------------
Div方法出现异常
异常的名称是:ArithmeticException
异常的原因是:/ by zero
------------------------------
本次测试共出现2次异常
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值