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:
- Static Global Initialization code
- Static Project Initialization code
- 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:
- 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).
- In the Test Parameters window, open Dynamic Analysis> Test Case Generation> Common.
- Double-click the Static Global Initialization, Static Project Initialization, or Static Class Initialization node. A Static Initialization window will open.
- Enter the initialization code in the Static Initialization window, or import the code by choosing Options> Import.
- To save the modification, choose Options> Save.
- 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
|