Jtest logo




Contents  Previous  Next  Index

Adding Test Classes


Test Classes let you add test cases that are too complex or difficult to be added as method inputs. For example, you might want to use Test Classes if you want to:

  • Use objects as inputs for static and instance methods.
  • Test a calling sequence and check the state of the object using asserts.
  • Create complicated test cases that depend upon a specific calling sequence.
  • Validate the state of an object.

A Test Class is a class that extends jtest.TestClass and is used to specify test cases that Jtest should use to test the class. For information about jtest.TestClass, see the Jtest API javadoc (you can access this documentation by choosing Help> Jtest API). The jtest.TestClass file is in the jtest.zip file located in <jtest_install_dir>/classes.

Test Classes give you an easy way to write complicated test cases and to verify the state of the objects under test. You can write your own Test Classes using any Java development environment, or you can integrate JUnit classes into your Jtest tests.

To add Test Classes, you need to:

  1. Set you environment to use Test Classes.
  2. Create a Test Class (or locate a JUnit Test Class)
  3. Load the Test Class (this is not necessary if you have named and saved the Test Class in the conventional manner).

These steps are described in detail below.

Setting Your Environment to Use Test Classes

In order to use the Test Classes feature, you need to add the jtest.zip file that contains jtest.TestClass to your CLASSPATH. This zip file is located in <jtest_install_dir>/classes/jtest.zip

You can have Jtest automatically set your CLASSPATH by opening a command prompt, changing directories to the Jtest installation directory, then entering the appropriate command(s):

  • If you are using Windows, enter:
    $ jtvars.bat
  • If you are using a bash or sh shell, run the jtvars.sh script in the Jtest installation directory. For example,
    $ cd <jtest-home>
    $ . jtvars.sh
  • If you are using a csh, tcsh, or ksh shell. source the jtvars script in the Jtest installation directory. For example,
    $ cd <jtest-home>
    $ source jtvars
  • To determine which shell you are using, enter
    $echo $SHELL

Creating Test Classes

Test Classes should specify test cases by using a public static void method for each test case. The correct behavior of the class should be specified using "assert (String message, boolean condition)" statements.

For example:

 public class TestVector extends jtest.TestClass
 {
     public static void testSize ()
     {
         Vector vector = new Vector ();
         vector.addElement("name");
         assert ("should be 1", vector.size () == 1);
     }

Jtest will consider methods within the class that are not static public methods to be helper methods. These methods can be called to do initialization for the test case.

A Test Class can contain any number of test cases.

For examples of Test Classes, see <jtest_install_dir>/examples/dynamic/testclasses.

Naming Conventions

It is convenient to name Test Classes so that the word "Test" appends the class name. For example, if you want to create a Test Class for the class called "foo", you should name your Test Class "fooTest". You should also save this Test Class in the same package as the class under test. If you follow both of these recommendations and have set your CLASSPATH as described above, Jtest will automatically locate the Test Class when you start a test.

For example, if you test the java.util.Vector class, Jtest will automatically search on the CLASSPATH for a java.util.VectorTest class. If it finds this class, it will use it as the Test Class for the java.util.Vector class.

You may also use a different name and/or save the class in a package other than the one that contains the class under test; the only requirement is that the class is on your CLASSPATH. However, if you use a different name or package, you need to manually indicate that Jtest should use this Test Class (as described below in Loading Test Classes).

Defining Stubs within a Test Class

When you are using a Test Class, you can define stubs for each test method by defining a "stubs()" methods within the Test Class. For example, to specify the stubs for a test case defined by a "testXYZ" method, define a method of the form:

Object stubsXYZ (Method method, ...);

in that Test Class.

If a Test Class does not define a stubs method, or if it does not return any stubs, Jtest will apply the Class and Project Test Parameters "stubs()" methods.

For more information on User-Defined Stubs, see Using Custom Stubs

Using JUnit Test Classes

If you want to use a JUnit Test Class with Jtest, you need to:

  • Include the junit.jar file on your CLASSPATH.
  • Manually indicate the location of your Test Class (as described in Loading Test Classes below).

After you perform these steps, Jtest will use the JUnit Test Class when you run your test in the normal manner.

JUnit 3.5 is supported.

Note: The Jtest Tutorial contains a lesson on using JUnit Test Classes with Jtest. To reach this tutorial, choose Help> Tutorial in either Jtest UI.

Loading Test Classes

If you name your test <classname>Test, saved it in the same package as the class under test, and set your CLASSPATH, Jtest will automatically find and load it (this is described in the above section).

To manually indicate which Test Class(es) Jtest should use for the current class:

  1. Right-click the Class Test Parameter tree's Dynamic Analysis> Test Case Generation> User Defined> Test Classes node. A shortcut menu will open.
  2. Choose Add Test from the shortcut menu.
  3. Enter the name of your Test Class in the text field.

Checking Test Classes

If you want to check your test class:

  1. Control-click the Class Test Parameter tree's Dynamic Analysis> Test Case Generation> User Defined> Test Classes node.
  2. Choose Check from the shortcut menu.

Running Test Classes

Within Jtest

When Jtest performs dynamic analysis, it will execute all Test Classes that it finds automatically and/or you load manually.

If any assertion fails, Jtest will report an error in the Specification and Regression Errors branch of the Results panel or Errors Found panel.

If any test case throws an uncaught runtime exception, Jtest will report an error in the Uncaught Runtime Exceptions of the Results panel or Errors Found panel.

Note: When Jtest executes a test case within the test class, all of the Test Class's static variables will be initialized with default values.

Outside of Jtest

You can run a Test Class outside of the Jtest environment by entering the following command at the command line:

 java jtest.TestClass <your TestClass>

For example, if your Test Class was named fooTest, you would enter the following command:

 java jtest.TestClass fooTest

Important: In order to execute this command, you must have the jtest.zip file in your CLASSPATH.

Testing Projects That Include Test Classes

If you are testing a project that includes Test Classes, by default, Jtest will not perform static or dynamic analysis on any class that it recognizes as a Test Class.

Testing a Test Class

By default, Jtest does not perform static or dynamic analysis on any class that it recognizes as a Test Class. If you want Jtest to test a Test Class, open the class's Class Test Parameters window, then enable the Common Parameters> Test the Test Class itself option.

Related Topics

About Black-Box Testing

Performing Black-Box Testing

Adding Method Inputs

Specifying Imports

Jtest Tutorials


Contents  Previous  Next  Index

ParaSoft logo
(888) 305-0041 info@parasoft.com Copyright © 1996-2001 ParaSoft