Debugging During Robolectric Test Execution In Android Studio

I’ve been exploring testing in Android Studio via Robolectric and wanted to check the values of some variables in the code under test. I tried using System.out.println() to print the values, expecting that when I ran my test gradle task I’d see the output in my terminal, since I run my tests via gradle’s cli. What I got was the same test failure, but no output from my println() call.

For my next attempt, I added a breakpoint and ran the tests via the same cli command, but that didn’t work either. After a bit of searching I learned that I can debug a gradle task easily by finding the task in Android Studio’s Gradle tool window and selecting debug (you can right-click, or use the Run menu, or use the CTRL-SHIFT-D keyboard shortcut.

Android Studio Debug Icon

What I learned next is that doesn’t work right way, because by default Android Studio executes gradle via an in-process build, resulting in the following error:

Unable to open debugger port : java.net.SocketException “Socket closed”

In order to get around this, you need to disable the “Use in-process build” feature via the Compiler/Gradle preferences.

Android Studio "in-process build" pref

After I did that, I was able to debug the gradle test task, and see the output of my state in the Android Studio Debug tool window.

Now I’m back to having fun writing tests!

Thanks to Eugene who figured this out and posted the answer to his own question on Stackoverflow!

Join the Conversation

1 Comment

Leave a comment

Your email address will not be published. Required fields are marked *