Junit’s Theory’s as interprested by Schauderhaft and Groovy

JUnit theories sound promising.  Many a time a developer writes a whole lot of @Tests along the lines of testParameterXzero(), testParameterXone(), testParameterXmaxInt(). The test code may be almost identical apart from the parameters being used in the class/method under test which is redundant and prone to error.

Theories offer a sound alternate, specifying a single test method, with a different set of annotations to the regular @Test defining a field or annotation based ParameterSupplier to inject a series of values into each test.

Schauderhaft’s blog provides a great summary of what theories are and how to write a parameter supplier to supply a series of datapoints in Java. The annotation based ParameterSupplier does look a little verbose but as Schauderhaft points out, they can be reused – annotation based data fixtures – excellent. I liked these two posts because they do a better job at explaining than the release notes. The blog also links to a blog about another Junit4 feature – parameterised tests – that seem to be a precursor to Theories, and a little simpler to setup. Another great source of how to use Theories is actually in the Groovy documentation about Junit integration. Closures make Datapoints a little more concise (or maybe the example is just simpler? hehe) along with the fact that test data (lists,maps) have a groovy conciseness to them to begin with.

Some Groovy Little Tips

My favourite Groovy/Java/IntelliJ web presenter, Vaclav Pech, has a short 20min Parleys talk on using Groovy with IntelliJ.

What I found neat was IntelliJ’s ability to take a Java class, rename it as a Groovy one, then refactor all the annonymous inner classes.  I also learnt about the capabilities of Groovy, the part where a class gets defined as a Map.  Wonderful stuff.

Also neat, was showing how refactorings apply not only to Groovy and Java, but also to Scala code in the same project.

http://beta.parleys.com/#state=state_player&slide=7&id=354

Scratch your Groovy itch on the web

Here is a web based Groovy console, so the next time you are say on the train and want to write some Groovy using your iPhone* or at some PC with a locked down environment, you can!

http://groovyconsole.appspot.com/

Hours of fun or your money back :-p

* Nope, never wanted to write Groovy on the train either, but you never know.

Update from a former colleague of mine:

BTW, this is hosted on Google App engine, which only recently started supporting Java! There still seem to be some issues, I can’t execute code like System.properties{e,k->println e} due to some security exceptions. Similalry File based code also seems to be violating some google App Engin security.

Groovy 1.6

You could read thru the Jira release notes for 1.6 and 1.6rc1-3 or you could just read this infoq article which provides a summary of all the good stuff in the new Groovy release.

I dont think the Groovy home page does a good enough job of selling the new features 🙂

Chuffed

I just deployed my first Grails app running with PostgreSQL on MorphExchange.

Following their Java Dev Cookbook was quite easy though I found I had to do the following:

  • Under log4j configuration add a property logs.home = ”./logs” (here)
  • Don’t need to change the Grails DEFAULT_DEPS in War.Groovy to exclude jdbc2_0-stdext.jar per their doc.  Perhaps the exception they warn about will be a problem if trying to use JNDI?

The real wide internets beta lookout, I now have a place to host some unsuspecting Java onto the world, <cold evil laugh>