Archivo diario: 04/05/2015

Debugging tests with Gradle and Eclipse

If you use Gradle to run your tests, and are an Eclipse user but the project you are working in is not an Eclipse project, you might think that you are out of luck when it comes to debugging. But that’s not the case, you can use Eclipse to debug a test you can launch with Gradle.

Step #1: launch the test in Gradle

Launch your test with Gradle. Supposing your test is x.y.MyTest, run Gradle as follows:

gradle test --debug-jvm --tests x.y.MyTest

Because you are using the –debug-jvm thing, Gradle will wait for a debugger to get attached, as shown below:

Gradle waiting for a debugger to be attached

1. Launch the test with Gradle

STEP #2: create a Java launcher for remote debugging

To atttach the Eclipse debugger, open Eclipse and go to Run|Debug Configurations…, and create a new launcher: select the Remote Java Application type. The key point here is to use the same port Gradle is using, in this case 5005. I have called this debug launcher Gradle, debug, and have configured it as follows:

Configuring Eclipse to debug Gradle tests

2. Configure Eclipse to debug the Gradle test

Step #3: tell the launcher where to find the source code

To tell Eclipse where to find the source code, go to the Source tab in the launcher configuration. Click Add…, and choose the directory for your Java source code.

3. Tell Eclipse where to find the source code

3. Tell Eclipse where to find the source code


Now you can use Eclipse to place some breakpoints, step through your code, etc. Just open the launcher and click Debug. Voilá!

What else?

I have tested this with Gradle 2.3 and Eclipse Luna (4.4.2).