-
Notifications
You must be signed in to change notification settings - Fork 0
/
UnitTestsEx.java
143 lines (86 loc) · 3.76 KB
/
UnitTestsEx.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
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
139
140
141
142
143
/* File Header
*/
/* Class header
*/
import java.util.*;
public class UnitTestsEx {
// Instance Variables
// Constructors
/* Method header */
public UnitTestsEx () {}
// TODO Methods (Write your Unit Tests here.)
/** exampleTestUsingPrinting
Example test case. Uses println very often.
May be useful if you prefer your unit test to
"speak" to you.
NOTE: The return type of this method is void.
All the results are printed to terminal.
*/
public void exampleTestUsingPrinting(){
System.out.println("Unit Test Example. ");
ExampleClass ec = new ExampleClass();
String correctAnswer = "9y^2";
String result = ec.derivative(3, 'y', 3);
String correctAnswer2 = "5";
String result2 = ec.derivative(5, 'x', 1);
//The derivative of 5x^0
//The derivative of 5x
//The answer should be 5.
//However, the function might return 0x^-1. That's not right.
//Fix that error in the method, and then rerun this tester.
System.out.println(
"The correct answer is: " + correctAnswer + "\n" +
"The answer received is: " + result + "\n"
);
System.out.println(
"The correct answer is: " + correctAnswer2 + "\n" +
"The answer received is: " + result2 + "\n"
);
}
/** exampleTestUsingExceptions
If the program crashes due to the test case, an Exception
will be thrown. However, here is the important difference:
you may now edit your method and fix the code that causes
the Exception. Then, when you run the test again, if the
Exception is not thrown anymore, then the code would be fixed.
Exceptions are also possibly useful in minimizing the amount
of lines printed to the Terminal.
Note the return type is void. If a test case fails, then
an Exception will be thrown. Also note, that the example of
throwing an Exception took in an argument: a String.
Run this method and see what the Exception prints out the Terminal.
*/
public void exampleTestUsingExceptions() throws Exception {
ExampleClass ec = new ExampleClass();
String correctAnswer = "9y^2";
String result = ec.derivative(3, 'y', 3);
String correctAnswer2 = "5";
String result2 = ec.derivative(5, 'x', 1);
//The derivative of 5x^0
//The derivative of 5x
//The answer should be 5.
//However, the function might return 0x^-1. That's not right.
//Fix that error in the method, and then rerun this tester.
System.out.println("Running tests using Exception.");
if ( !correctAnswer.equals(result) ){
throw new Exception("First test failed.");
}
if ( !correctAnswer2.equals(result2) ) {
throw new Exception("Second test failed.");
}
}
/** Using print statements to debug.
There is one more way of testing your code in debugging.
You can scatter print statements within your code.
This allows you to see the values change as the code runs.
This type of debugging is not a unit test, but it was worth
mentioning that this technique exists.
*/
// Main Method
public static void main (String[] args) throws Exception {
UnitTestsEx me = new UnitTestsEx();
//Write your method calls below.
//me.exampleTestUsingPrinting(); //TODO Try me.
//me.exampleTestUsingExceptions(); //TODO Try me.
}
}