Auto-generating Unit-Tests for legacy Java-code

Not free. Not opensource. But I have found AgitarOne Agitator (http://www.agitar.com/solutions/products/agitarone.html) to be REALLY good for automatically generating unit tests AND looking for unwanted obscure side effects


It is interesting, but such generated unit tests can actually be useful. If you're working on a legacy application, it will be often hard to write correct, state-of-the-art unit tests.

Such generated tests (if you have a way of generating them of course) can then make sure that behavior of code stays intact during your changes, which then can help you refactor the code and write better tests.

Now about generating itself. I don't know about any magic tool, but you may want to search for JUnit functionality about including some tests in javadocs for methods. This would allow you to write some simple tests. And yes, it's actually of some value.

Second, you can just write "big" tests by hand. Of course, these wouldn't be unit tests per se (no isolation, potential side-effects, etc), but could be good first step. Especially if you have little time and a legacy application.

Bonus Tip! There is an excellent book "Working effectively with legacy code" with examples in Java, including techniques right to use in such situations. Unfortunately you would have to do some things manually, but you would have to do that at some step anyway.