注記フレームワークのテストケース

12432 ワード

Calculatorクラスファイル
public class Calculator {
    @Chack
    public static void add(){
        System.out.println("1 + 0 = " + (1 + 0));
    }
    @Chack
    public static void sub(){
        System.out.println("1 - 0 =" + (1 - 0));
    }
    @Chack
    public static void mul(){
        System.out.println("1 + 0 =" + (1 * 0));
    }
    @Chack
    public static void div(){
        System.out.println("1 + 0 =" + (1 / 0));
    }

}

実装テスト
/
public class DemoMain {
    public static void main(String[] args) throws IOException {
        // Calculator , 
        Calculator c = new Calculator();
        Class cls = c.getClass();
        Method[] methods = cls.getMethods();
        // 
        int number = 0;
        // bug.txt 
        BufferedWriter bw = new BufferedWriter(new FileWriter("bug.txt"));
        // Calculator 
        for (Method method : methods) {
            if(method.isAnnotationPresent(Chack.class)){
                try {
                    method.invoke(c);
                } catch (Exception e) {
                    // bug.txt 
                    bw.write(method.getName() + " ");
                    bw.newLine();
                    bw.write(" :" + e.getCause().getClass().getSimpleName());
                    bw.newLine();
                    bw.write(" :" + e.getCause());
                    bw.newLine();
                    bw.write("-------------------");
                    bw.newLine();
                    number++;
                }
            }
        }
        bw.write(" " + number + " ");
        bw.close();

    }
}


Check注記ファイル
@Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
public @interface Chack {
}