A {@link TestCase} that can define both simple and bulk test methods.
A
simple test method is the type of test traditionally supplied by by {@link TestCase}. To define a simple test, create a public no-argument method whose name starts with "test". You can specify the the name of simple test in the constructor of
BulkTest
; a subsequent call to {@link TestCase#run} will run that simple test.
A
bulk test method, on the other hand, returns a new instance of
BulkTest
, which can itself define new simple and bulk test methods. By using the {@link #makeSuite} method, you canautomatically create a hierarchal suite of tests and child bulk tests.
For instance, consider the following two classes:
public class TestSet extends BulkTest { private Set set; public TestSet(Set set) { this.set = set; } public void testContains() { boolean r = set.contains(set.iterator().next())); assertTrue("Set should contain first element, r); } public void testClear() { set.clear(); assertTrue("Set should be empty after clear", set.isEmpty()); } } public class TestHashMap extends BulkTest { private Map makeFullMap() { HashMap result = new HashMap(); result.put("1", "One"); result.put("2", "Two"); return result; } public void testClear() { Map map = makeFullMap(); map.clear(); assertTrue("Map empty after clear", map.isEmpty()); } public BulkTest bulkTestKeySet() { return new TestSet(makeFullMap().keySet()); } public BulkTest bulkTestEntrySet() { return new TestSet(makeFullMap().entrySet()); } }
In the above examples,
TestSet
defines two simple test methods and no bulk test methods;
TestHashMap
defines one simple test method and two bulk test methods. When
makeSuite(TestHashMap.class).run
is executed,
five simple test methods will be run, in this order:
- TestHashMap.testClear()
- TestHashMap.bulkTestKeySet().testContains();
- TestHashMap.bulkTestKeySet().testClear();
- TestHashMap.bulkTestEntrySet().testContains();
- TestHashMap.bulkTestEntrySet().testClear();
In the graphical junit test runners, the tests would be displayed in the following tree:
- TestHashMap
- testClear
- bulkTestKeySet
- bulkTestEntrySet
A subclass can override a superclass's bulk test by returning
null
from the bulk test method. If you only want to override specific simple tests within a bulk test, use the {@link #ignoredTests} method.
Note that if you want to use the bulk test methods, you
must define your
suite()
method to use {@link #makeSuite}. The ordinary {@link TestSuite} constructor doesn't know how tointerpret bulk test methods.
@author Matt Hall, John Watkinson, Paul Jack
@version $Id: BulkTest.java,v 1.1 2005/10/11 17:05:39 pents90 Exp $