Overview | Package | Class | Use | Tree | Deprecated | Index | Help
PREV CLASS NEXT CLASS | FRAMES NO FRAMES
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD

## j2meunit.framework
Class TestCase

    java.lang.Object
      |
      +--j2meunit.framework.Assert
            |
            +--j2meunit.framework.TestCase

All Implemented Interfaces:
  Test

Direct Known Subclasses:
  TestAll, TestOne, TestTwo

public abstract class TestCase
extends Assert
implements Test

A test case defines the fixture to run multiple tests. To define a test case

1. implement a subclass of TestCase
2. define instance variables that store the state of the fixture
3. initialize the fixture state by overriding `setUp`
4. clean-up after a test by overriding `tearDown`.

Each test runs in its own fixture so there can be no side effects among test runs. Here is an example:

     public class MathTest extends TestCase
     {
         protected int nValue1;
         protected int nValue2;

         protected void setUp()
         {
             nValue1= 2;
             nValue2= 3;
         }
     }

For each test implement a method which interacts with the fixture. Verify the expected results with assertions specified by calling `assertTrue` (or another assert method) with a boolean:

         public void testAdd()
         {
             double result= nValue1 + nValue2;
             assertTrue(result == 5);
         }

Once the methods are defined you can run them. The framework supports both a static type safe and more dynamic way to run a test. In the static way you override the runTest method and define the method to be invoked. A convenient way to do so is with an anonymous inner class:

     TestCase test= new MathTest("add")
     {
         public void runTest()
         {
             testAdd();
         }
     };
     test.run();

In JUnit, the dynamic way uses reflection in the default implementation of `runTest`. Because reflection is not available in J2ME, here a different approach is necessary. Together with a method name, an instance of the class TestMethod can be given to the constructor of TestCase. It can be created as an anonymous inner class that invokes the actual test method in the implementation of the

    run

method:

     TestCase test = new MathTest("testAdd", new TestMethod()
     { public void run(TestCase tc) { ((MathTest) tc).testAdd(); } });
     test.run();

To make this work subclasses need to implement the additional constructor and forward the parameters to the corresponding TestCase constructor. Multiple tests to be run can be grouped into a TestSuite. J2MEUnit provides different test runners which can run a test suite and collect the results. Because J2ME doesn't provide reflection, a test suite must always be created manually from test instances associated with TestMethods. The missing reflection is also the reason that the

    suite

method cannot be static in J2MEUnit:

     public Test suite()
     {
         TestSuite suite = new TestSuite();
         {
         suite.addTest(new MathTest("testAdd", new TestMethod()
         { public void run(TestCase tc) {((MathTest) tc).testAdd(); } }));
         suite.addTest(new MathTest("testAdd", new TestMethod()
         { public void run(TestCase tc) {((MathTest) tc).testDivideByZero(); } }));

         return suite;
     }

See Also:
  `TestResult`, `TestSuite`, `TestMethod`

Constructor Summary
`TestCase()` Default constructor.
`TestCase(java.lang.String name)` Constructor for a named Test.
`TestCase(java.lang.String name, TestMethod testMethod)` Constructor for a test that will execute a particular test method.

Method Summary
`int` | `countTestCases()` Always return 1.
`int` | `countTestSteps()` Returns the same as countTestCases.
`protected TestResult` | `createResult()` Creates a default TestResult object
`java.lang.String` | `getName()` Returns the test name.
`TestMethod` | `getTestMethod()` Returns the Method to be executed by the test case instance.
`java.lang.String` | `getTestMethodName()` Deprecated. Replaced by getName()
`protected void` | `onAssertion()` Callback from the Assert base class that will be invoked on assertions.
`TestResult` | `run()` A convenience method to run this test, collecting the results with a default TestResult object.
`void` | `run(TestResult result)` Runs the test case and collects the results in TestResult.
`void` | `runBare()` Runs the bare test sequence.
`protected void` | `runTest()` The default implementation will run the TestMethod associated with the TestCase instance and asserts that it is not null.
`void` | `setName(java.lang.String name)` To set the test name.
`void` | `setTestMethod(java.lang.String methodName, TestMethod testMethod)` Convenience method to set the name and wrapper of the method to be executed by the test case instance.
`void` | `setTestMethod(TestMethod testMethod)` To set the method to be executed by the test case instance.
`void` | `setTestMethodName(java.lang.String name)` Deprecated. Replaced by setName(String)
`protected void` | `setUp()` Sets up the fixture, for example, open a network connection.
`Test` | `suite()` This method should be overridden if a test case contains multiple test methods.
`protected void` | `tearDown()` Tears down the fixture, for example, close a network connection.
`protected void` | `testStepFinished()` Notifies listeners that a test step has finished.
`java.lang.String` | `toString()` Returns a string representation of the test case.

Methods inherited from class j2meunit.framework.Assert
`assertEquals, assertEquals, assertEquals, assertEquals, assertNotNull, assertNotNull, assertNull, assertNull, assertSame, assertSame, assertTrue, assertTrue, fail, fail, failNotEquals, failNotSame`

Methods inherited from class java.lang.Object
`clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait`

Constructor Detail

### TestCase

    public TestCase()

  Default constructor.

### TestCase

    public TestCase(java.lang.String name)

  Constructor for a named Test.

Parameters:
  `name` - The name of the test (method)

### TestCase

    public TestCase(java.lang.String name,
                    TestMethod testMethod)

  Constructor for a test that will execute a particular test method.

Parameters:
  `name` - The name of the test method
  `testMethod` - The TestMethod wrapper for the method to execute

Method Detail

### setName

    public void setName(java.lang.String name)

  To set the test name.

  Parameters:
    `name` - The new test name
  Since:
    1.1

### getName

    public java.lang.String getName()

  Returns the test name.

  Returns:
    The test name
  Since:
    1.1

### setTestMethod

    public void setTestMethod(java.lang.String methodName,
                              TestMethod testMethod)

  Convenience method to set the name and wrapper of the method to be executed by the test case instance.

  Parameters:
    `methodName` - The name of the test methoto execute
    `testMethod` - The method wrapper

### setTestMethod

    public void setTestMethod(TestMethod testMethod)

  To set the method to be executed by the test case instance.

  Parameters:
    `testMethod` - The TestMethod to execute

### getTestMethod

    public TestMethod getTestMethod()

  Returns the Method to be executed by the test case instance.

  Returns:
    A TestMethod instance

### setTestMethodName

    public void setTestMethodName(java.lang.String name)

  Deprecated. Replaced by setName(String)

  Deprecated, replaced by setName(String).

  Parameters:
    `name` - The test instance name

### getTestMethodName

    public java.lang.String getTestMethodName()

  Deprecated. Replaced by getName()

  Deprecated, replaced by getName().

  Returns:
    The test instance name

### countTestCases

    public int countTestCases()

  Always return 1.

  Specified by:
    `countTestCases` in interface `Test`
  Returns:
    1

### countTestSteps

    public int countTestSteps()

  Returns the same as countTestCases.

  Specified by:
    `countTestSteps` in interface `Test`
  Returns:
    The number of test steps
  See Also:
    `Test.countTestSteps()`

### run

    public TestResult run()

  A convenience method to run this test, collecting the results with a default TestResult object.

  See Also:
    `TestResult`

### run

    public void run(TestResult result)

  Runs the test case and collects the results in TestResult.

  Specified by:
    `run` in interface `Test`
  Parameters:
    `result` - The TestResult to collect the data in

### runBare

    public void runBare()
                 throws java.lang.Throwable

  Runs the bare test sequence.

  Throws:
    `java.lang.Throwable` - if any exception is thrown

### suite

    public Test suite()

  This method should be overridden if a test case contains multiple test methods. It must return a TestSuite containing the test methods to be executed for this test case. This TestSuite can be constructed as below:

       new TestSuite(new MyTestCase("testMethodOne", new TestMethod()
       {   public void run(TestCase tc)
           { ((MyTestCase) tc).testMethodOne(); }
       }));

  Returns:
    A new test suite

### toString

    public java.lang.String toString()

  Returns a string representation of the test case.

  Overrides:
    `toString` in class `java.lang.Object`
  Returns:
    The name of the test case or, if not set, the class name

### setUp

    protected void setUp()
                  throws java.lang.Exception

  Sets up the fixture, for example, open a network connection. This method is called before a test is executed.

  Throws:
    `java.lang.Exception` - An arbitrary exception may be thrown

### createResult

    protected TestResult createResult()

  Creates a default TestResult object

  See Also:
    `TestResult`

### runTest

    protected void runTest()
                    throws java.lang.Throwable

  The default implementation will run the TestMethod associated with the TestCase instance and asserts that it is not null. To use the version 1.0 test case style override this method (but don't call super!) and invoke the method corresponding to the name of the TestCase instance. Since there is no reflection this must be done by a string compare and an explicit call of the necessary method.

  Throws:
    `java.lang.Throwable` - if any exception is thrown

### tearDown

    protected void tearDown()
                     throws java.lang.Exception

  Tears down the fixture, for example, close a network connection. This method is called after a test is executed.

  Throws:
    `java.lang.Exception` - An arbitrary exception may be thrown

### onAssertion

    protected void onAssertion()

  Callback from the Assert base class that will be invoked on assertions.

  Overrides:
    `onAssertion` in class `Assert`
  See Also:
    `Assert.onAssertion()`

### testStepFinished

    protected void testStepFinished()

  Notifies listeners that a test step has finished.

Overview | Package | Class | Use | Tree | Deprecated | Index | Help
PREV CLASS NEXT CLASS | FRAMES NO FRAMES
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD