| Example
JUnit Test Fall 2006, David Matuszek |
Here is the method largest, which we want to test.
public class SaddlePoint {
int largest(int[] array) {
return 9999;
}
} |
Here is the test for largest.
import junit.framework.TestCase;
public class SaddlePointTest extends TestCase {
// DECLARE your frequently-used variables here
int[] array1;
int[] array2;
SaddlePoint saddle;
protected void setUp() throws Exception {
super.setUp();
// Give your variables values here
// DON'T DECLARE them here, or they will be local to this method!
array1 = new int[] { 3, 7, -6, 10, 4 };
array2 = new int[] { -1, -2, -3, -4 };
saddle = new SaddlePoint();
}
public final void testLargest() {
assertEquals(10, saddle.largest(array1));
assertEquals(-1, saddle.largest(array2));
}
} |
However, notice that largest used no instance variables, hence
it might as well have been static. If it is static,
then we don't need to create an instance of SaddlePoint to
refer to it; instead, we refer to the class (SaddlePoint) that contains the
method largest. Like so:
public class SaddlePoint {
static int largest(int[] array) {
return 9999;
}
} |
Here is the test for largest.
import junit.framework.TestCase;
public class SaddlePointTest extends TestCase {
// DECLARE your frequently-used variables here
int[] array1;
int[] array2;
|