Skip to content

Commit 40b9484

Browse files
committed
docs: adds instructions for debugging Groovy scripts.
1 parent 32f553f commit 40b9484

File tree

5 files changed

+68
-1
lines changed

5 files changed

+68
-1
lines changed

docs/groovy_debugging.md

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
# Debugging Groovy scripts
2+
3+
You can attach a debugger to the JVM to step through your Groovy scripts, inspect variables etc.
4+
5+
The key steps involved are:
6+
7+
1. Enable the JVM debugger, listening on a local port
8+
2. Start Imposter
9+
3. Connect to the debug port
10+
11+
## Enable the JVM debugger
12+
13+
Enable the JVM debugger with the following environment variable:
14+
15+
JAVA_TOOL_OPTIONS="-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=8000"
16+
17+
> **Note**
18+
> Here we have set the debug port to 8000.
19+
20+
### Docker users
21+
22+
When running in Docker, make sure to expose the debug port.
23+
24+
For example:
25+
26+
```bash
27+
docker run --rm -it -p8080:8080 -p8000:8000 -v $PWD:/opt/imposter/config outofcoffee/imposter
28+
```
29+
30+
> In this example, port 8000 is exposed along with the regular port, 8080.
31+
32+
## Steps for IntelliJ IDEA
33+
34+
These steps will explain how to connect your IDE's debugger to the debug port.
35+
36+
In IntelliJ, create a simple project pointing to the directory containing your script file.
37+
38+
In the following steps, we will add a new run configuration, start your mock and attach the debugger.
39+
40+
### Step-by-step
41+
42+
1. Add a 'Remote JVM Debug' run configuration:
43+
![groovy_jvm_debug_config.png](images/groovy_jvm_debug_config.png)
44+
2. Set the `Port` to the same value as the debug configuration.
45+
3. Start Imposter and wait for the server to be ready.
46+
4. In IntelliJ start the run configuration in Debug mode, which will connect the debugger to the Imposter JVM.
47+
48+
Set breakpoints in the script file as normal. When your script is invoked in Imposter, the debugger will pause on breakpoints and you can evaluate variables in scope.
49+
50+
![groovy_debug.png](images/groovy_debug.png)

docs/groovy_tips.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@
44

55
This section provides additional tips and tricks when using Groovy as the scripting language for your mocks. It builds on the [Scripting](./scripting.md) documentation. If you are new to Imposter scripting, it's best to start there.
66

7+
## Debugging Groovy scripts
8+
9+
See the [debugging Groovy scripts](./groovy_debugging.md) documentation.
10+
711
## Block syntax for `respond`
812

913
Groovy users can use this special block syntax, for improved readability:
@@ -99,3 +103,15 @@ To do this:
99103

100104
1. create a directory containing the JAR file
101105
2. set the `IMPOSTER_PLUGIN_DIR` environment variable (or set `plugin.dir` in your `.imposter.yaml` file in the config directory) to the path above
106+
107+
## Improved Groovy DSL support in your IDE
108+
109+
For more sophisticated script development, you can set up a project in your IDE with improved code assistance. You can use this alongside [the debugger](./groovy_debugging.md) for a fully integrated development experience.
110+
111+
This requires setting up a project with a dependency on `imposter-api.jar` and adding the GroovyDSL (GDSL) file.
112+
113+
➡️ [See an example project](https://github.com/outofcoffee/imposter/tree/main/examples/groovy-dsl)
114+
115+
If you just want to download the GDSL file see:
116+
117+
➡️ [Download the GDSL file](https://github.com/outofcoffee/imposter/tree/main/examples/groovy-dsl/src/main/resources/imposter.gdsl)

docs/images/groovy_debug.png

678 KB
Loading
341 KB
Loading

docs/scripting.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -265,5 +265,6 @@ This selects the example from the OpenAPI `examples` section for the API respons
265265

266266
## Further reading
267267

268-
* [Tips and tricks for Groovy users](./groovy_tips.md)
268+
* [Tips and tricks for Groovy scripts](./groovy_tips.md)
269+
* [Debugging Groovy scripts](./groovy_debugging.md)
269270
* [Using modern JavaScript in scripts](./scripting_modern_js.md)

0 commit comments

Comments
 (0)