Jtest logo




Contents  Previous  Next  Index

Setting an Object to a Certain State


In some cases, you may want to set up an initial state prior to testing a class. For example, suppose that a class is used as a global object containing static member variables accessible by any other project within the application. When Jtest tests an object that uses this static member variable, a NullPointer Exception will result because the variable has not been set. This problem can be solved by giving Jtest static initialization code.

About Static Initialization Code

All initialization code will be executed before any test case is executed, and can be used to setup and initialize the class if needed.

You can add static initialization code at the global, project, or class level. Initialization code set in the Global Test Parameters will be executed for all classes that Jtest tests. Initialization code set in the Project Test Parameters will be executed for all classes in the project. Initialization code set in the Class Test Parameters will be executed only for the class whose parameters you are editing.

Initialization code is executed in the following order:

  1. Static Global Initialization code
  2. Static Project Initialization code
  3. Static Class Initialization code

For an example that uses initialization code, see <jtest_install_dir>/examples/dynamic/common/ClassInit.

Note: Initialization code can only be used to invoke static methods.

Adding Static Initialization Code

To add static initialization code:

  1. Open the Test Parameters window for the level at which you want to add initialization code.
    • To add Global Initialization code, click Global (in either UI).
    • To add Project Initialization code, click Project (in the Project Testing UI).
    • To add Class Initialization code, click Class (in the Class Testing UI).
  2. In the Test Parameters window, open Dynamic Analysis> Test Case Generation> Common.
  3. Double-click the Static Global Initialization, Static Project Initialization, or Static Class Initialization node. A Static Initialization window will open.
  4. Enter the initialization code in the Static Initialization window, or import the code by choosing Options> Import.
  5. To save the modification, choose Options> Save.
  6. To exit the Static Initialization window, choose Options> Quit.

Related Topics

About Dynamic Analysis

Performing Dynamic Analysis

Customizing Dynamic Analysis

Testing Classes That Reference External Resources

Using Custom Stubs


Contents  Previous  Next  Index

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