-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSampleCodeTest.java
More file actions
138 lines (124 loc) · 5.17 KB
/
SampleCodeTest.java
File metadata and controls
138 lines (124 loc) · 5.17 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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
import java.io.IOException;
/**
* This test class includes many examples of how to test a piece of code.
* It also provides the general flow of how the TestController is used:
* instantiate the object, add tests, run all the tests, and generate the report.
*/
public class SampleCodeTest {
public static void main(String[] args) throws IOException {
TestController tc = new TestControllerImpl("results.html");
// Check int
Test testInt = new Test() {
@Override
public TestResult runTest() {
CodeToBeTested code = new CodeToBeTested();
int returnedVal = code.addOne(0);
if (returnedVal == 1) {
return TestResult.createPassedResult();
} else {
return TestResult.createFailedResult("addOne expected to return 1");
}
}
};
tc.addTest(testInt, 1.0);
// Check double that fails due to precision
Test testDoubleFail = new Test() {
@Override
public TestResult runTest() {
CodeToBeTested code = new CodeToBeTested();
double returnedVal = code.returnsHalf(Math.PI);
if (returnedVal == 1.570796326794897) {
return TestResult.createPassedResult();
} else {
return TestResult.createFailedResult("returnsHalf expected to return half of pi");
}
}
};
tc.addTest(testDoubleFail, 2.0);
// Check double that works
Test testDouble = new Test() {
@Override
public TestResult runTest() {
CodeToBeTested code = new CodeToBeTested();
double returnedVal = code.returnsHalf(Math.PI);
if (returnedVal > 1.570796326794896 && returnedVal < 1.570796326794898) {
return TestResult.createPassedResult("returnsHalf works");
} else {
return TestResult.createFailedResult("returnsHalf expected to return half of pi");
}
}
};
tc.addTest(testDouble, 3.0);
// Check returned object value
Test testObjectValue = new Test() {
@Override
public TestResult runTest() {
CodeToBeTested code = new CodeToBeTested();
CodeToBeTested returnedVal = code.returnsObject();
if (returnedVal.getVariableToChange() == false) {
return TestResult.createPassedResult("testObjectValue");
} else {
return TestResult.createFailedResult("testObjectValue expected to find object's value to be false");
}
}
};
tc.addTest(testObjectValue, 4.0);
// Check returned object equals
Test testObject = new Test() {
@Override
public TestResult runTest() {
CodeToBeTested code = new CodeToBeTested();
String returnedVal = code.returnsString();
if (returnedVal.equals("false")) {
return TestResult.createPassedResult("testObject");
} else {
return TestResult.createFailedResult("testObject expected the String 'false'");
}
}
};
tc.addTest(testObject, 5.0);
// Checking a modified instance variable
Test testSideEffect = new Test() {
@Override
public TestResult runTest() {
CodeToBeTested code = new CodeToBeTested();
code.modifyVariable();
if (code.getVariableToChange()) { // variable successfully changed
return TestResult.createPassedResult("Value was true");
} else {
return TestResult.createFailedResult("Value was false");
}
}
};
tc.addTest(testSideEffect, 12.0);
// Check for an anticipated exception
Test testAntiException = new Test() {
@Override
public TestResult runTest() {
CodeToBeTested code = new CodeToBeTested();
try {
code.methodThrowsException(-1);
} catch (IllegalArgumentException e) {
return TestResult.createPassedResult("Correctly caught exception");
}
return TestResult.createFailedResult("testAntiException: Failed to catch exception");
}
};
tc.addTest(testAntiException, 15.0);
// Failed to check for an exception
Test testException = new Test() {
@Override
public TestResult runTest() {
CodeToBeTested code = new CodeToBeTested();
code.methodThrowsException(-1);
// Should never get to this part of the code
return TestResult.createFailedResult("testException should have thrown an exception");
}
};
tc.addTest(testException, 16.0);
// run tests
tc.runTests();
// get report
tc.createReport();
}
}