Java Test Harness

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:

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:

http://junit.sourceforge.net

Test Harness Constraints

Component Testing for Java complies with most JUnit test cases. However, it introduces the two following constraints:

You must be especially aware of these constraints when importing existing test cases into Rational Test RealTime

Naming Conventions

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.

Test Class Primitives

The test class defines the primitives that create and test the objects under test. The test class primitives are:

void setUp() throws Exception

setUp:

 

void tearDown() throws Exception

tearDown:

 

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.

Running a Test

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

Component Testing for JavaJ2ME Specifics