-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathMyTestRunner.java
67 lines (60 loc) · 2.08 KB
/
MyTestRunner.java
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package customize_annotations_generics_wildcards_examples;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.List;
public class MyTestRunner {
public void run(Class<?>... klasses) {
for (Class<?> testClass : klasses) {
runTestClass(testClass);
}
}
//an overloaded method to the above one, so that it could accept a list of classes, not just a simple array of classes
public void run(List<Class<?>> testCaseClasses) {
for (Class<?> testClass : testCaseClasses) {
runTestClass(testClass);
}
}
private void runTestClass(Class<?> klass) {
for (Method method : klass.getMethods()) {
SteveSunFirstCustomAnnotation annotation = method
.getAnnotation(SteveSunFirstCustomAnnotation.class);
if (annotation != null)
runTestMethod(klass, method, annotation);
}
}
private void runTestMethod(Class<?> klass, Method method,
SteveSunFirstCustomAnnotation annotation) {
if (annotation.state() != MyTestState.ACTIVE)
return;
try {
System.out.println("Running test: "
+ getTestName(method, annotation));
Object testInstance = klass.newInstance();
method.invoke(testInstance);
System.out.println("SUCCESS");
} catch (InstantiationException e) {
System.err.println("FAILED: Failed to instantiate class "
+ klass.getName());
} catch (IllegalAccessException e) {
System.err.println("FAILED: Failed to call test method "
+ method.getName());
} catch (InvocationTargetException e) {
checkThrowable(annotation, e.getCause());
}
}
private static String getTestName(Method method,
SteveSunFirstCustomAnnotation annotation) {
return !annotation.name().isEmpty() ? annotation.name() : method
.getName();
}
private void checkThrowable(SteveSunFirstCustomAnnotation annotation,
Throwable th) {
if (annotation.expected() == th.getClass())
System.out.println("annotation.expected() executes: SUCCESS");
else if (annotation.ignore() == th.getClass()) {
System.out.println("annotation.ignore() executes: SUCCESS");
} else {
System.out.println("FAILED: " + th.getMessage());
}
}
}