Java Reference
In-Depth Information
5.
//
The constructor initializes the counter to 0
6.
public
CounterModel() {
7.
value = 0;
8.
}
9.
10.
public void
increment(){
11.
value++;
12.
}
13.
14.
public void
decrement(){
15.
value--;
16.
}
17.
18.
public void
reset(){
value = 0;
19.
}
20.
21.
22.
public int
getValue(){
return
(value);
23.
}
24.
}
25.
A counter is such a simple object that we can immediately 'see' that our imple-
mentation is correct, i.e. meets the specifications. For slightly more complicated
entities, however, one can easily overlook a mistake. The problem of checking
whether a program meets its specifications is hard to solve, in fact it is in gen-
eral unsolvable. Therefore one has to rely on
empirical tests
. This means that
one sets up a
test plan
. This contains a (large) number of possible inputs to the
program and the correct responses of the program. The plan should activate all
parts of the program, e.g. all methods of all classes. Then one checks whether the
observed and expected responses coincide. Of course, such a plan is no guarantee
that the program is indeed correct because it might not contain an existing input
that causes an error. However, a good test plan often does discover mistakes in the
program.
To give an idea of what a test plan can look like we add the listing of class
Coun-
terModelTest
which implements a test plan for the counter model. It addresses
all methods at least once and compares the expected and observed results. The
comparison is done by method
checkValue(a,b)
, which compares
a
and
b
and
prints the result of the comparison to the screen.
File:
its/CounterGUI/CounterModelTest.java
package
its.CounterGUI;
1.
2.
3.
4.
public class
CounterModelTest {
Search WWH ::
Custom Search