-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTestMain.java
More file actions
29 lines (25 loc) · 1.04 KB
/
TestMain.java
File metadata and controls
29 lines (25 loc) · 1.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
package chapter6.annotations;
import java.lang.reflect.Method;
import java.util.Objects;
public class TestMain {
public static void main(String[] args) throws ReflectiveOperationException {
int tests = 0;
int passed = 0;
Class<?> aClass = Class.forName("chapter6.annotations.Sample"); // 可修改为从main函数传参
Object o = aClass.getDeclaredConstructor().newInstance();
for (Method method : aClass.getMethods()) {
Test annotation = method.getAnnotation(Test.class);
if(Objects.nonNull(annotation)) {
try {
method.invoke(o);
System.out.printf("%s通过测试\n", method.getName());
passed++;
} catch (Exception e) {
System.err.printf("%s测试异常: %s", method.getName(), e.getCause().getMessage());
}
tests++;
}
}
System.out.printf("对%s个方法进行测试,%s个方法测试通过\n", tests, passed);
}
}