Create custom Java code



Search Tips   |   Advanced Search



Custom code uses references in the test as input and returns modified values to the test.


Interfaces include...

These interfaces are contained in the package...

Results are displayed in the test log.

Verification point verdicts, are reflected in the overall schedule verdict.

Input values and references

Custom code input values are located under references or field references. These references must be included in the tests. Within the tests, the references must precede the code that they affect. Verify that the test contains the references required for customized inputs to code.

If custom code uses external JAR files, set the Java build path.

In some cases, you can avoid changing the build path manually by running the test before adding custom code to it. The first time a test runs, classes and libraries required for compilation are added to the build path.

For example, you can import Test and Performance Tools Platform (TPTP) classes that are needed to create custom events in the test log if the test, to which you have added custom code, has run previously. However, if the test has never been run, import errors occur because the classes are not named in the build path for the project until the test has run.

If code uses external resources, for example, an SQL database or a product that manages customer relationships, configure the custom code to work on each computer on which test runs.

Custom code resides in the src folder of the project containing the test that calls the code. By default, custom code is located in a package named "test" inside the src folder.

Here is an example of the Navigator view of two custom code classes. Note that the Test Navigator view does not display Java source files.

If and are added to, and return a value to, the test, then Substitute from will list those two classes as data source options...

The "test" package also contains the generated Java code for tests in the project. You can put custom code in a different package other than "test", for example, "custom".

Separate custom code from generated code, especially if you use a source-control system.

Add custom code

  1. Open the test and select a test element.

  2. Click Add or Insert, and select Custom Code.

    Add appends the custom code to the bottom of the selected test element. Insert adds the custom code above the selected test element.

    After you add or insert custom code, the Problems view displays an error stating that the new custom code element has no Java file. This error message remains until you click View Code or Generate Code, to remind you that the custom code test element is not yet associated with any Java code.

  3. Inspect the Class name field, and complete one of the following steps.

    • If the code to call already exists, change the class name to match its name.

      Click View Code to open the code in the Java editor.

    • If the code does not exist, change the class name to describe the purpose of the code.

      Click Generate Code to generate a template class for logging results and to open it in the Java editor. If a class with this name exists, you are warned that it will be overwritten.

  4. In the Arguments field, click Add.

  5. In the Custom Code window, select all inputs that code needs.

    The Custom Code window lists all values in the test that can be used as inputs to code (references or field references in the test that precede the code).

  6. Click OK.

    The window closes, the selected references are added to the Arguments field.

  7. In the test, after custom code, locate a value that code returns to the test.

  8. Highlight the value.

  9. Right-click the highlighted value, click Substitute from, and select the class name of custom code.

    The custom code classes that you have added are listed. After you have made selection, the value to be returned to the test is highlighted in orange and the Used by table is updated with this information.

Custom code is not displayed in the Test Navigator view. To see, open view...