-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTestRunner.java
More file actions
114 lines (108 loc) · 2.73 KB
/
TestRunner.java
File metadata and controls
114 lines (108 loc) · 2.73 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
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
/**
* Class to provided testing utility. Custom unit testing framework. Used in place
* of Junit4 due to its incompatibility with Zylabs.
*
* @author Stephen
* @version 2019-02-06
*/
public class TestRunner
{
/**
* The test classes to run. E.g. if you have a class named "Animal", you likely have
* a test class named "AnimalTest". You should add "AnimalTest" along with any other
* test classes to this array.
*
* THIS IS ONLY PART OF THIS CLASS THAT YOU SHOULD MODIFY.
*/
private static String[] testClasses = {
//"ContestantTest",
//"ContestantInfoTest",
//"HandChoiceTest",
"RegionTest",
//"RPSArenaTest"
};
/**
* Main method. DO NOT MODIFY.
*
* @param args Program args.
*/
public static void main(String[] args)
{
for (String className : testClasses)
{
// Track if tests pass:
boolean allPassed = true;
try {
// Get an instance of the class from the name:
Object classInstance = Class.forName(className).newInstance();
// Get all methods declared (i.e. not inherited) from the class:
Method[] methods = Class.forName(className).getDeclaredMethods();
// Run all tests, assuming that there are no helper methods:
for (Method mth : methods)
{
boolean passed = true;
Exception error = null;
// Try to execute the method:
try {
mth.invoke(classInstance);
}
catch(InvocationTargetException e)
{
passed = false;
error = e;
}
catch(IllegalAccessException e)
{
passed = false;
error = e;
}
catch(IllegalArgumentException e)
{
passed = false;
error = e;
}
// Report if test passed or not:
if (passed)
{
System.out.println(mth.getName() + " ---- passed");
}
else
{
System.out.println(mth.getName() + " ---- failed");
error.printStackTrace(System.out);
System.out.println("\n");
allPassed = false;
}
}
}
catch (ClassNotFoundException e)
{
String errorMessage = String.format("Could not find the class: %s, check the testClasses variable"
+ "and make sure the name matches exactly.", className);
System.out.println(errorMessage);
allPassed = false;
}
catch (InstantiationException e)
{
e.printStackTrace();
allPassed = false;
}
catch (IllegalAccessException e)
{
allPassed = false;
e.printStackTrace();
}
// Report if test passed or not:
if (allPassed)
{
System.out.println(className + " ---- passed");
}
else
{
System.out.println(className + " ---- failed");
}
}
}
}