Unit tests!

In the morning, I had a very interesting discussion with a colleague of mine. He was writing a simple handler which passed an annotation to a calling class about which we knew nothing. All we knew that by passing the annotation, the class did stuff properly. For those fortunate enough to have dealt with objects in python and ruby, behold the Java annotation example:

public void testSomethingWorks() {
    assertTrue("Eve knows ", alice.secret(), bob.secret());

More can be found at Java 1.5 Annotations

The @Test is the annotation, telling the whatever is reading the code that what follows is a JUnit test. Similarly we had an annotation, above a class method and the discussion started. My friend’s argument was  something that only did one thing, and that thing was just an annotation passing to a black box, did not need a equally trivial unit test. Now this test is made harder by absence of any decent mocking frameworks. That is, we could have mocked out the external utility that was a black box and done a fast and simple JUnit test, but not to be.

I asked my friend if he cared when/if the code was changed. On being answered in the affirmative, I asked him whether in that situation, he would like to know by our build light breaking, or by one of our system administrators coming to us and informing us?

Thankfully, that seemed to convince him that a test, whether trivial or not, is needed to cover a piece of code which does something. Any thing. I say thankfully because I really did not have any more arguments up my sleeve, other than my sanctimonious anger 😛

About “Mujtaba Hussain”

I have no idea what to write here :(