Using the TestCase class

Component Testing for Java

To create a test case in the Component Testing for Java test harness, create a test class by deriving from the TestCase of the test harness. Only classes derived from TestCase can use the test harness services

Adding Test Primitives

To add a new test, use the setup and teardown methods to create and configure objects under test.

Such objects belong to the test class. The setup method allows you to configure the objects under test. The teardown frees the objects.

Running a Test Case

The most convenient way to invoke a test case is to use the constructor with the test name as an argument. For example:

TestCase TestStocksObject = new TestStocks("testStocksValues");

TestCase TestStocksObject2 = new TestStocks("testStocksAmount");

 

This way, the TestCase object automatically call the public method name that was passed as an argument with the run call.

To use this technique in J2ME, you must first create a runTest() method in the test class which will call the correct function.

Examples

The following series of examples shows how to test a simple class Stocks in the J2SE framework. First, derive the test class from TestCase to check the arithmetic methods:

package examples;

import junit.framework.*;

import examples.Stocks.*;

public class TestStocks extends TestCase {                      

public TestStokcs(String name) {

super(name);

}

public void testStocks1() {

Stock first = new Stock(“Company,”Dollar”,100,1.25);

Stock second = new Stock(“Company,”Dollar”,250,1.25);

Stock added = new Stock(first + second);

//Display a message in the report.

verifyLogMessage("Check equals for the count of stocks");

verifyEquals("verify equals added count",

added.amountstocks(),

(first.amountstocks()+second.amountstocks()));

}

 

An equivalent implementation for J2ME would be:

import j2meunit.framework.*;

import examples.Stocks.*;

public class TestStocks extends TestCase {

public TestStocks(String name) {

super(name);

}

protected void runTest() throws java.lang.Throwable {

if(getTestMethodName().equals("testStocksAmount"))

testStocksAmount ();

else if(getTestMethodName().equals("testStocksValues"))

testStocksValues();

}

 

This test class checks for a thrown exception:

public void testException3()

{

verifyLogMessage("Check true for RTE");

Throwable toverify= new Throwable("StocksError");

verify(toverify);

//This rate conversion  will thow a StockError Exception.

Stock divided = new Stock(first.convertwithrate(0));

}

 

The following example demonstrates an object vector verification:

public void testAccounts()

{

  Vector RefAccountStocks = new Vector();

  RefAccountStocks .addElement( new Stocks(“Company,”Dollar”,100,1.25));

  RefAccountStocks .addElement(new Stocks(“Company2,”Dollar”,100,2.68));

  verifyLogMessage("Verify Equality for Accounts");

  verify("verify equal vector", RefAccountStocks, OtherAccountStocks );

}

 

Component Testing for Java allows you to check timing between events by using the time method of the TestCase class:

public void testTimerOnStocks()

{

  int idtimer1;

  idtimer1 = createTimer("first timer created");

  

  //then start the timers.

  timerStart(timer1,"Start 1");

  long val1;

//Unit is ms.

  val2 = 100;

  verifyLogMessage("Timer report Transaction");

  timerReportEllapsedTime(timer1,"First report of time before the action");

  Stock dynamicalAccount = first.extractfromWebSite(second);

  verifyEllapsedTime(timer1,val1,"ellapsed 1 with 100");

}

 

In J2SE, run the tests by calling the run method of the test class:

TestResult result = TestStockObject.run() ;

 

TestResult result = new TestResult();

TestStockObject.run(result) ;

 

In J2ME, you run the test class by calling the run method of the object under test:

TestResult result = TestStockObject.run() ;

 

TestResult result = new TestResult();

TestStockObject.run(result) ;

 

Related Topics

Using the TestResult ClassUsing the TestSuite ClassJ2ME Specifics