Jetif Quick Start

Vicky Wang


Here is a simple example showing you the steps to create your own tests using Jetif. Since it's a quick start document, it's only some brief introduction about basic features of Jetif, please refer to the User's Guide for more information.

Test Case

To create a test case, you should create at least a test class, which contains some test methods. Just like the following:

public class MyFirstTest {
    public boolean testMethod1() {
        return true;
    }
}

Is it very simple? Oh, yes, but there is no test code. We should add some test code to the test method,for example, to test a Account class we may add some code like following:

public class MyFirstTest {
    public boolean testMethod1() throws Exception {
        Account account = new Account("Smith");	// a new account with balance 0.
        account.deposit(100);	// deposit $100
        return account.getBalance() == 100;
    }
}

Above code will works if there is a Account class exist with properly implementation. To run the test, just start the Jetif in command line:

java -cp %CLASSPATH% jetif.ui.Console class:MyFirstTest

Where the %CLASSPATH% should be replaced by properly classpath contains the jetif.jar, xerces.jar and test classes. A result file named "MyFirstTest.txt" will be generated under the folder which you start the command when it finished, and it will shows a test case named "testMethod1" passed.

Note: Both boolean and void are allowed as the return type in the simple test method here, please refer to the User's Guide for more information.

Suite

Usually, it is desired to create more than one test cases for an application, organize them and run them at once. For this purpose, we introduced the "suite" concept. A suite consists of a number of test cases. The test cases of a suite will be ran at once.

A suite spec is a runtime object to define a suite, it's loaded before a suite run, and provides specification about the suite. A suite loader is required to load a suite spec by the framework. There are some built-in loaders are supported by Jetif: XML spec file loader, Simple Class suite loader and JUnit suite loader. While it is allowed to provide custom loader which load suite spec from other sources.

The test method of the above simple test case can only use hard-coded values as the parameters of the method to be tested, but the Jetif allows the developer pass the parameter at runtime. All runtime parameters, and expected result for test case should be defined in the suite spec, so we are able to change the parameter without re-compiling the test code. To receive parameters at runtime, the test method should takes some parameters. For example:

public class MyFirstTest {
    public int testMethod2(int input) throws Exception {
        Account account = new Account("Smith");	// a new account with balance 0.
        account.deposit(input);
        return account.getBalance();
    }
}

The following is the XML profile, the XML representation of suite spec (sample.spec):

<?xml version="1.0" encoding="ASCII"?>
<suite name="MyFirstSuite" class="jetif.ext.BasicSuite">
  <properties>
    <property name="test.class">MyFirstTest</property>
  </properties>
  <testcase name="Test Case1">
    <method name="testMethod1">
      <expected>true</expected>
    </method>
  </testcase>
  <testcase name="Test Case2">
    <method name="testMethod2">
      <param>50</param>
      <expected>50</expected>
    </method>
  </testcase>
</suite>

To run the suite, start following command at command line, where the %CLASSPATH% should be replaced by properly classpath contains the jetif.jar, xerces.jar and test classes:

java -cp %CLASSPATH% jetif.ui.Console sample.spec

You will got a result file named "MyFirstSuite.txt" under the folder you start the suite.

Tips: A test case may have more than one test methods.

Download

Click here to download the package of related files.


Translations: 简体中文


Copyright © 2004,2005 Vicky Wang, All Rights Reserved.