Component Testing for Java
Component Testing for Java generates a full Java test harness based on JUnit-compliant classes for J2SE and J2ME framework.
The Java test harness can be used for single thread component testing, using the following main JUnit classes:
TestSuite: This class is a container for multiple test classes derived from TestCase
TestCase: The basic class that is derived into a series of user-defined test classes
TestResult: This class returns the results of a given test class:
Unexpected errors: unwanted exceptions
Failed assertions: produced by the JUnit assert test primitives
Failed verifications: produced by the extended verify test primitives
The list of extended verify test primitives can be found in the Reference section.
Please refer to the JUnit documentation for further information about JUnit assert primitives as well as general JUnit documentation. This can be found at the official JUnit Web site:
Component Testing for Java complies with most JUnit test cases. However, it introduces the two following constraints:
User test classes must derive from the TestCase class, or from a TestSuite that contains one or several TestCase classes
The test harness cannot be applied to multi-threaded Java components
You must be especially aware of these constraints when importing existing test cases into Rational Test RealTime
Test class names should be prefixed with Test, as in
Test<ClassUnderTest>
Where <ClassUnderTest> is the name of the class under test. This naming convention enables the test class to use test class primitives.
Test method names must be prefixed with test, as in:
test<TestName>
Where <TestName> is the name of the test.
The test class defines the primitives that create and test the objects under test. The test class primitives are:
Creation of the objects under test: This primitive must create and initialize all the objects under test.
void setUp() throws Exception
setUp:
End of test: Use this primitive to insert any code that is required to end the test, such as to set any setUp created objects to null.
void tearDown() throws Exception
tearDown:
Test Primitives: The test class must also define as many test<TestName> methods as there are tests.
You can inject such a TestCase into a TestSuite. This way, The TestSuite automatically creates as many TestCases as requires and executes a sequential run of all the tests.
To run a series of tests, you must incorporate a main inside a TestCase or TestSuite class, build the main, the TestSuite and TestClass, and execute the run.
In J2ME, these objects can be built in a midlet, which contains only TestSuite and TestCase, and launches the run on the start app primitive. If the test case was generated by Test RealTime, you must comment the main method that was automatically generated.
Example
To test that the sum of two Moneys with the same currency contains a value which is the sum of the values of the two Moneys, write:
public void testSimpleAdd() {
Money m12EUR=new Money(12, "EUR");
Money m14EUR=new Money(14, "EUR");
Money expected= new Money(26, "EUR");
Money result= m12EUR.add(m14EUR);
assertTrue(expected.equals(result));
}
Related Topics