Product Development in Brisbane

Technique for getting JUnit tests to compile

I have just started reading Dave Astels book on TDD.

He talks about the fact that when working in Java, the first step is to make the tests compile, and that IDEs help to do that.

He also mentions that in a method that has a return type you must return something. This is mostly correct, but doesn’t match what I have found to be a very useful approach. I am going to jump into java 101 quickly to help describe what I do.

A java method with a return type must return something of that type, or throw a Throwable. The Throwable(Exception) needs to be of a type mentioned in the throws clause of the method signature, or it needs to be an Unchecked exception.

This brings me to my point….

When I am writing the method stub required to make a test compile, I make it throw an Unchecked Exception. This makes it pretty obvious that this has not been implemented.

I have been using something like this:

public boolean isSomething(){
throw new NoSuchMethodError(”Has not been implemented”);
}

I find this communicates better where the code is at than:

public boolean isSomething(){
return false;
}

I have updated my intelliJ code templates to do this.

(ctrl-alt-s, n, code, New method body)

Leave a Reply