| Subcribe via RSS or via Email

How to unit test Java code and not lose your mind, part one

September 23rd, 2007 Posted in Groovy

Two of the universal truths about programming are; everyone knows that they should be unit testing, and that everyone (well everyone I know) hates unit testing.

Imagine the situation; you have a complex package of Java classes which you aren’t quite sure are working right. What do you do? You know that unit testing would be the right thing to do and although tools such as eclipse and JUnit make the process easier, its still going to be dull and time consuming. So much so that you wont bother unit testing and instead you will keep fixing bugs as they crop up, possibly causing new ones in the process (go on admit it you’ve been there and bought the t-shirt).

But what if you could combine unit testing with something useful like learning a cool new programming language . You just might be inclined to actually do some unit testing then. But what if as well as learning a new cool new language, you were also able to use the features of this new language to enable you to produce unit tests faster and which are more readable than if you were using plain old Java. And what if you could do all of this without scaring your boss by bringing in radical new language that requires its own runtime. Then you might actually get quite excited about unit testing. Well excited is probably the wrong word, enthusiastic is probably better (if you do indeed get excited about unit testing then I would suggest that you get professional help or take a holiday).

All this is possible with Groovy. If you haven’t heard about groovy then I suggest that you to to the groovy home page and read a bit about it before you carry on. It may also be useful to read my last post about Groovy.

The first things to remember about Groovy when unit testing are that JUnit is embedded in it and that a GroovyTestCase (which is an extension of a JUnit test case) has 11 extra assertion methods in addition to the standard JUnit ones. For more information see the page on Groovy unit testing.
Unit testing Java code from Groovy is just as easy as unit testing Java code from Java; remember that its all the same Java bytecode in the end.

Ok now its time for an example. Lets unit test my Java Fibonacci code from before with Groovy.

First write the Groovy test cases:

class JavaFibonacciTest extends GroovyTestCase

{
 void testFibResults()
 {
//some standard assertions from the JUnit TestCase
 	assertEquals(SpeedTestJava.fib(0), 0)  //fib 0 = 0
 	assertEquals(SpeedTestJava.fib(1), 1)  //fib 1 = 1
 	assertEquals(SpeedTestJava.fib(2), 1)  //fib 2 = 1
 	assertEquals(SpeedTestJava.fib(3), 2)  //fib 3 = 2
 	assertEquals(SpeedTestJava.fib(4), 3)  //fib 4 = 3

 }

 void testFibResults2()
 {
 	//some assertions from the GroovyTestCase
 	//assertToString converts the result to a string by calling its toString() method
 	assertToString(SpeedTestJava.fib(0), '0')
 	assertToString(SpeedTestJava.fib(4), '3')

 }

}

To run this you can simply use the command:

groovy JavaFibonacciTest.groovy

or you can compile the Groovy code using:

groovyc JavaFibonacciTest.groovy

and then run it using:

groovy JavaFibonacciTest

Either way the output will look like this:

..

Time: 0.04OK

(2 tests)

And that’s it. You’ve written a Test case for Java in groovy. Notice that you didn’t need a main method, or a constructor as you would have done to run the same code as a JUnit test case. Its also import to note that Groovy test cases don’t need a package declaration, for reasons which I don’t totally understand.

I think that’s about enough for now. In part two I will explain how to organise Groovy tests into test suites and how to integrate them with eclipse.

Leave a Reply

retaggr