Download Wiley - Java Tools for Extreme Programming

Transcript
This method wraps a call to a method requiring protection. (Note: this method throws Throwable.)
Interface Test
public interface Test
Description
Part of the Composite design pattern, Test represents the common interface for single (TestCase), composite
(TestSuite), and special (TestDecorator) tests. The shared interface allows all types of tests to be treated
similarly by the framework.
Methods
countTestCases()
public int countTestCases()
This method counts the number of TestCases that will be run by this test (1 for TestCases, or the grand total
of contained TestCases for suites).
run(TestResult result)
public void run(TestResult result)
This method runs this test and collects the results in the TestResult parameter.
Class TestCase
public abstract class TestCase
Inheritance Information
Extends: Assert
Implements: Test
Known subclasses: ExceptionTestCase
Description
TestCase is the central class of the JUnit framework. Subclasses of TestCase form the building blocks of
automated test suites in JUnit. A TestCase subclass is usually designed to exercise the public interface of the
class under test.
The standard template for a subclass is a String constructor, a setUp() method, a tearDown() method, and any
number of (public) test methods named according to the form test<name of method to be tested>(). The
setUp() method ensures that the test fixture is initialized, and the tearDown() method performs cleanup after a
given test. When tests are run, each test method is run as part of a separate instance of the TestCase to
which it belongs. The series of steps for test method execution is:
setUp()
<testMethod>
tearDown()
Note
A test fixture is loosely defined as all the data and/or objects the test code needs in
order to run properly. A TestCase for a sorting method might have a test fixture
consisting of several unsorted arrays and a properly instantiated Sorter object.
Constructor
TestCase(String name)
public TestCase(String name)
TestCase() constructs an instance of the TestCase based on the name argument. TestCase follows the
Pluggable Selector design pattern, in which the behavior of an object can be modified externally. In the case
of TestCase, this means each TestCase instance will run only one of its test methods when a caller invokes
run(). Unless the behavior of runTest() is overridden, the name of the TestCase specifies which test method is
executed.
Suppose a method testHeatUp() exercises heatUp() on a Weather object. To run this test method in the JUnit
framework, we create a new TestCase with the following syntax:
344