Java Reference
In-Depth Information
Test t=
new
Test();
t.testInstance()
The first line creates an instance of class
Test
and stores its name in a new vari-
able
t
. The second line calls
t
's function
testInstance
, which yields
5
.
I.2.3
Javadoc
DrJava has a facility for creating and displaying Javadoc comments. Click the
item
Javadoc
on the right of the tool bar at the top of the DrJava window. A nav-
igation window appears, which asks you to select a folder into which the Javadoc
files will be placed. We suggest that you create a new folder titled “doc” within
the folder for the project you are working with and select it. After you select the
folder, DrJava creates the Javadoc files —be patient; it can take a few moments.
Finally, a new window will appear with the Javadoc spec in it, in the same for-
mat as the Java API specifications.
I.2.4
Using JUnit in DrJava
The application JUnit is designed to facilitate the testing of Java programs. JUnit
can be used in many contexts; here, we show how to use it in DrJava.
We use as an example the testing of function
max
of class
SimpleMath
in
Fig. I.2, which is assumed to be in file
SimpleMath.java
. We have deliberately
public class
SimpleMath {
/** =
maximum of
x
and
y */
public static int
max(
int
x,
int
y) {
if
(x <= y)
{
return
y - 1; }
return
x;
}
}
import
junit.framework.TestCase;
public class
TestMax
extends
TestCase {
public void
testXBigger()
{ assertEquals(7, SimpleMath.max(7, 5)); }
public void
testYBigger()
{ assertEquals(5, SimpleMath.max(-5, 5)); }
public void
testXYSame()
{ assertEquals(8, SimpleMath.max(8, 8)); }
}
Figure I.2:
Using JUnit to test method
max
Search WWH ::
Custom Search