|
J avolution v5.2 (J2SE 1.5+) | ||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectjavolution.testing.TestCase
public abstract class TestCase
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 prepare() { ... } // Optional
// Executes the test (it may exercise the test case several times).
public void execute() { ... } // Mandatory.
// Returns the number of times the test case has been exercised (default 1).
public int count() { ... } // Optional
// Validates the results.
public void validate() { ... } // Optional.
// Cleanups after execution (e.g. release resources).
public void cleanup() { ... } // Optional
}
TimeContext
)
may run the sequence (prepare, execute, validate, cleanup) multiple
times to calculate for example the average execution time (
validation
in that case is performed only once
after the last run). Here is an example of test case
implementation for the HashMap.put(key, value)
method:
Test cases are typically grouped into a
class HashMap_put extends TestCase {
private HashMap _map;
private int _size;
public HashMap_put(int n) {
_size = 0;
Index.setMinimumRange(0, n); // Preallocates.
}
public void prepare() {
_map = new HashMap();
}
public void execute() {
for (int i=0; i < _size;) {
_map.put(Index.valueOf(i), Index.valueOf(i++));
}
}
public int count() { // Number of put operations performed.
return _size;
}
public void validate() {
TestContext.assertTrue("Wrong size", _size == _map.size());
for (int i=0; i < _size;) {
if (!TestContext.assertEquals(_map.get(Index.valueOf(i)), Index.valueOf(i++)))
break; // Error, no need to continue.
}
// Asserts performance.
long avgTime = TimeContext.getAverageTime("ns");
TestContext.assertTrue(avgTime + "ns too slow!", avgTime < 100);
}
public CharSequence getDescription() {
return "java.util.HashMap.put(key, value) - " + n + " entries added";
}
};
TestSuite
:
public HashMapTests extends TestSuite {
public void run() {
TestContext.info("Test put(key, value) for various size");
TestContext.test(new HashMap_put(10));
TestContext.test(new HashMap_put(100));
TestContext.test(new HashMap_put(1000));
...
}
}
TimeContext.enter(); // To measure execution time.
try {
new HashMapTests().run();
} finally {
TimeContext.exit();
}
TestContext
Constructor Summary | |
---|---|
protected |
TestCase()
Default constructor. |
Method Summary | |
---|---|
void |
cleanup()
Cleanup once test is complete (the default implementation does nothing). |
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.CharSequence |
getDescription()
Returns the description of this test case or null if none. |
void |
prepare()
Prepares the test case execution (the default implementation does nothing). |
java.lang.String |
toString()
Returns the String representation of this test case
(the description or the class name by default). |
void |
validate()
Validates the test results (the default implementation does nothing). |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait |
Constructor Detail |
---|
protected TestCase()
Method Detail |
---|
public void prepare()
public abstract void execute()
count()
method should be overriden).
public int count()
1
).
execute()
.public void validate()
public void cleanup()
public java.lang.CharSequence getDescription()
null
if none.
null
public java.lang.String toString()
String
representation of this test case
(the description or the class name by default).
toString
in class java.lang.Object
|
J avolution v5.2 (J2SE 1.5+) | ||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |