Starting with version 2.0.0, the JMCF add-on comes with a Groovy script editor and tester in the configuration screens of the calculated custom field. This document details the Groovy script tester, its availability and features.
The Groovy script tester tool lets you test your written script against any issue. The main advantage of this tool is that you can quickly test and debug your script and make changes without having to actually save the custom field and recalculate the calculated field value.
Groovy script tester availability
The Groovy script editor in JMCF is available on the Custom field configuration screens of calculated custom field types that expect the result of a
Using the Groovy Script tester
After writing your Groovy script in the editor, click on the
Test Groovy Script button on the toolbar
. A modal dialog window opens, asking you to pick an issue to run the Groovy script against, as well as a linked issue, where applicable.
Selecting an issue
You can select the issue from one of the following options:
- Issue key: start typing an issue key, you will be offered options based on your issue browsing history
- [Select issue]: if you click on
[Select Issue], an Issue selector window gets displayed. You can select the issue either from:
- Recent Issues: Displays issues that you have recently viewed and first 50 issues from your current search or
- From Filter: Displays issues of your saved searches.
issue variable used in your script will point to the above-selected issue.
Testing your Groovy script
After selecting issue against which the script should be tested (as explained above), click on
Test. The following information will be displayed. This information can be used for debugging.
Message: Success/error message based on the test result.
- Result type: Data type of the result.
- Result value: Value of the result.
- Stack: If an exception was thrown during the execution of the script, a stack trace will be displayed.
- Logs: Information logged using the
Test a script that returns the sum of two number fields:
- Locate the custom field on the Custom Fields administration page.
- Click on the cog wheel and click on
- Click on
Edit Groovy Formula
In the Groovy editor, write the following lines of code:
issue.get("customfield_10114") + issue.get("customfield_10150")
- Click on the
Test Groovy Script button on the toolbar.
- Type an Issue key.
- Click on
- The following result is displayed:
Debugging your script
If you encounter an error during testing, you will need to debug your script. The Message, Stack and Log information displayed in the script tester result panel aids in debugging the script.
Using the Message and Stack trace
In the above example, after encountering the error,
Using the log variable
Using the log variable, you can debug your script, a step further, by adding information into the log. For example, while testing a condition in your script, if the result evaluates to
false when you expect it to be
true, you might want to know what values are being compared in your condition. In such cases, you can use the log variable to display the value.
The log calls should always be used before the return statement because they would never get executed after it.