public abstract class TestCase
extends java.lang.Object
This class represents a test case which can be used for validation, performance and regression tests.
The structure of a test case is as follow:
It should be noted that some testing contexts (e.g.
class MyTestCase extends TestCase {
// Prepares data/state in which to run the test.
public void setUp() { ... } // Optional
// Executes the test possibly exercising the function tested multiple times.
public void execute() throws Throwable { ... } // Mandatory.
// Returns the number of times the function tested has been exercised (default 1).
public int count() { ... } // Optional
// Validates the test results and possibly check for limit cases or exceptions.
public void validate() throws Throwable { ... } // Mandatory.
// Cleanups after execution (e.g. to release resources).
public void tearDown() { ... } // Optional
}
TimeContext
)
may run the sequence: setUp, execute and tearDown multiple
times to calculate for example the average execution time,
validation
in that case is performed only once
after the last execution.
public class TypeFormatParseInt extends TestCase {
static final int N = 1000; // Number of random samples.
int[] _expected = new int[N];
int[] _actual = new int[N];
String[] _strings = new String[N];
public void setUp() {
for (int i = 0; i < _expected.length; i++) {
_expected[i] = MathLib.random(Integer.MIN_VALUE, Integer.MAX_VALUE);
_strings[i] = String.valueOf(_expected[i]);
}
}
public void execute() {
for (int i = 0; i < N; i++) {
_actual[i] = TypeFormat.parseInt(_strings[i]);
}
}
public int count() {
return N;
}
public void validate() {
// Compares expected versus actual (for random values).
TestContext.assertArrayEquals(_expected, _actual);
// Supplementary tests to check for limit cases.
TestContext.assertEquals(Integer.MIN_VALUE, TypeFormat.parseInt("-2147483648"));
TestContext.assertEquals(0, TypeFormat.parseInt("0"));
TestContext.assertEquals(Integer.MAX_VALUE, TypeFormat.parseInt("2147483647"));
// Checks exceptions raised.
TestContext.assertException(NumberFormatException.class, new Runnable() {
public void run() {
TypeFormat.parseInt("2147483648"); // Overflow
}
});
TestContext.assertException(NumberFormatException.class, new Runnable() {
public void run() {
TypeFormat.parseInt("123E4"); // Invalid Character
}
});
}
}
Test cases may be run
individually or as part of a TestSuite
. If an error occurs
the location of the assert failing is usually available (a hyperlink
in Netbeans and Eclipse).
... > [test] TypeFormat.parseDouble(CharSequence) > [error] Array element at 840, expected 2.078139623637765E-308 but found 2.0781396236377647E-308 at javolution.TypeFormatTest$ParseDouble.validate(TypeFormatTest.java:419)
TestContext
Modifier | Constructor and Description |
---|---|
protected |
TestCase()
Default constructor.
|
Modifier and Type | Method and Description |
---|---|
int |
count()
The number of times the test case is exercised (default
1 ). |
abstract void |
execute()
Executes this test case (possibly multiple times in which case
the
count() method should be overriden). |
java.lang.String |
getName()
Returns the name of this test case.
|
TestCase |
ignore(boolean isIgnored)
Selects whether or not this test case should be ignored.
|
boolean |
isIgnored()
Indicates whether or not this test case should be ignored.
|
void |
setUp()
Prepares the test case execution (the default implementation does
nothing).
|
void |
tearDown()
Cleanup once test is complete (the default implementation does
nothing).
|
java.lang.String |
toString()
Returns the
String representation of this test case. |
abstract void |
validate()
Validates the test results and possibly checks for limit cases
or exceptions.
|
public java.lang.String getName()
public TestCase ignore(boolean isIgnored)
isIgnored
- true
if test case is ignored;
false
otherwise.public boolean isIgnored()
true
if this test case is ignored;
false
otherwise.public void setUp()
public abstract void execute() throws java.lang.Exception
count()
method should be overriden).java.lang.Exception
public int count()
1
).execute()
.public abstract void validate() throws java.lang.Exception
java.lang.Exception
public void tearDown()
public java.lang.String toString()
String
representation of this test case.toString
in class java.lang.Object
this.getName()
Copyright © 2005 - 2007 Javolution.