Test Your Code

Error rendering macro 'includeplus'

Hazelcast instance is not active!

Fill in the lines beneath setup:, when:, and then: blocks with code that tests your script. The particulars of doing that will vary a bit based on what you’re testing (an event handler, a REST Endpoint, a macro, etc.).

Once you’ve written your test, you can save it to one of your script roots and run it using the Running Tests task.

Test-Driven Workflow

When starting on a new feature, Adaptavist often uses a test-driven development workflow. This is the process:

  1. Write a test for a desired outcome.

  2. Run the test.

  3. The test probably fails.

    This indicates that the feature isn’t working yet.

  4. Implement the feature until the test passes.

  5. Do any required code cleanup.

    1. Refactor.

    2. Make sure the test still passes.

  6. Peer review.

  7. Make suggested edits.

  8. Test to make sure the code is running as expected.

Running Tests

There are two ways that you can run automated tests: through the Test Runner built-in script or via your IDE.

Test Runner Built-in Script

You can use the Test Runner built-in script to run JUnit and Spock tests on the current Jira instance through ScriptRunner. The script creates custom test packages, and selects which tests to run from the checklist, simplifying the testing process.

  1. From ScriptRunner navigate to Built-in Scripts > Test Runner.

  2. Enter the name of the package(s) you want ScriptRunner to scan in Packages.

    Packages should follow the format com.yourcompany.scriptrunner.test, with all tests classes saved in the com/yourcompany/scriptrunner/test directory in your script root.

    The first time you open Test Runner, there is a pre-filled sample package com.acme.scriptrunner.test. This package contains sample tests. Run these tests to see an example of successful and failed tests.

  3. Select which tests to run from the Tests list.

    This list shows all available tests in the specified package.

  4. Optionally, click Preview to see details of which tests will be executed.

  5. Click Run to run the selected tests on the current instance.

    Do not run tests in a production instance.

  6. The outcome of tests can be viewed under the Results tab.

To use the included tests as a basis for custom tests, check out the source code for the sample plugins. Alternatively, unzip the ScriptRunner jar itself and edit the tests. Make sure you are using a source control system, so that you can test on your dev instance, commit your changes, then update your working copy on your production system.

Running from an IDE

You can run tests from your IDE. Adaptavist has only tested with Intellij IDEA and recommend it.

A test runner executes the tests via REST in the running application (Jira, Confluence, Bitbucket etc). In order for the IDE to know which runner to use, you must annotate your test class using the @RunWith annotation.

For example:

groovy
import com.onresolve.scriptrunner.canned.common.admin.ScriptRunnerTestRunner import org.junit.runner.RunWith import spock.lang.Specification @RunWith(ScriptRunnerTestRunner) class TestRunnerSampleSpec extends Specification { def "test something"() { expect: true } }

When you add a new test class, you must build your project (mvn package). Since the tests are invoked locally, and the compiled class must be available, the IDE cannot know which test runner to use. After this initial build, you can add new test methods and execute them without rebuilding.

IDEA Configuration

The IDE runs your test locally, which executes a REST call to the app. Therefore the IDE needs to know the address of your running application, so it can tell the app to run the tests. To do this, you can modify the default setting for JUnit tests:

  1. Navigate to Run > Edit Configurations.

  2. Open the Templates drop-down on the left navigation and then select JUnit.

  3. For VM Options, add a property -Dbaseurl= pointing to the URL of the running application.

    You can omit this if you are running your application locally on port 8080, as in http://localhost:8080/confluence.

  4. Under Before Launch, delete the Build task using the minus button.

    It’s not necessary to rebuild the plugin after editing the tests because ScriptRunner will recompile the test if necessary.

  5. Uncheck the Activate Tool Window box.

    It’s not useful to switch to the tool window because any failures will be shown in the main Log window.

Any exceptions are shown in the tool window, but log messages are not redirected to it, so there may not be a need to look at the tool window.

Use the IDEA Test
  • You can run the code by clicking the annotations in the margin, as shown below:

  • The keyboard shortcut to run the code is to put the cursor in the test class name or method name, and then press Ctrl + Shift + F10 for Windows or Ctrl + Shit + R (on Mac OSX).

On this page