Thursday, August 1, 2013

Guard Your Java Code with AutoTest Tooling

This was originally posted on the InfoSpace Tech Blog.


I recently moved into doing Android development after spending a few years in Ruby and Node.js (after many, many years in .NET). I have worked hard to shorten my personal code-health feedback loop to be as real-time as possible, so I can fix problems while retaining flow. While many folks work on a set of changes and tests and run those tests every-so-often, I found it to be extremely helpful to get the feedback automatically every time I save changes to a file.

I have not found a suitable free solution for this type of thing for Java. The solutions I have found typically require Eclipse or IntelliJ as well, and I've been more productive sticking with Vim and Bash lately. That being said, I wanted a solution that could support both IDE users and Text Editor users, just like I've had in Ruby.

InfoSpace is happy to announce that we have released our Guard::Java project to the community, for free under an MIT-style license!

Guard::Java

Guard::Java is built on top of my favorite auto-testing framework for Ruby, called Guard, and provides automatic, background testing of your code as you are working, without you having to stop your workflow! It provides this capability through the following features:
  • It watches your source and test files for changes
  • When a file changes, it tries to find the unit test fixture/class related to that file that changed
  • It runs just those tests, seeing if any basic unit tests for just that file have been violated or are now fixed
  • It can (optionally) after a successful single-class run, run all tests as a regression pass to see if any other unexpected impacts result from the changes you are making

Guard also provides event and status notifications in various forms:
  • Detailed output to the Console (e.g., in Eclipse or IntelliJ) or Shell
  • Terminal / Shell title bar text
  • Color coding of the session name when using TMUX
  • Growl notifications (pop-up messages/toasts) with custom messages

How Does This Help Me?

I just work the way I normally would, and my system tells me if I broke something or if everything is still OK, without me lifting an extra finger! While Guard::Java comes with sensible defaults for most common scenarios, it can also be configured for your specific situation.

What About Android Tests?

Yes, running Android tests are a pain since they are deployed to a device/emulator and fully run. In this case, we have adopted the following workflow:
  • Guard::Java watches file changes and automatically compiles the code and/or tests on every save action to ensure nothing has broken
  • It has a prompt in your Console/Shell that you can simply hit ENTER on and it will build, deploy, and run your tests using the standard ant tasks (or your own customized tasks) and report status all along the way.

The Guardfile that is generated when you run guard init already has code to automatically find your Android SDK folder and add it to the classpath as well!

I Use Eclipse/IntelliJ - Do I Need This?

It depends. If you have it configured to autotest specific code with each save, then you may not need or want this. The nice thing is that we have used this in Eclipse and IntelliJ (configuring as an External Tool, for example) using a Console tab in the IDE. We also have used this with folks using Text Editors (Sublime Text, ViM, and TextMate) with success.

It Doesn't Do What I Need

We are happy to take suggestions and pull requests. Just open an issue--or better yet a Pull Request--from the repository at https://github.com/infospace/guard-java.

How Do I Get Started

See the README on the project page on GitHub to get started!

Suggestions, feedback, and ideas are always welcome!