JPz'log Coin Coin and Plop da Plop

17Oct/080

JUnit and its clones are dead

If you had been using JUnit, TestNG or any other similar testing framework then you must stop doing so in your next projects. Seriously.

"Behavior driven development" has been in the air for some time now. The very gross idea is that unit tests often suck when you have to read them. Instead, you would prefer expressing how your classes should behave under various scenarios, like: "when something is done, this should happen". In turn those stories become immediately natural to read. You can of course write JUnit tests that are easy to read and understand, but this requires more discipline.

I recently came across Easyb, a BDD framework written in Groovy. It provides a very cool internal DSL for writing such scenarios / specifications.

Let's have a look at a small example. We write a very stupid calculator class in Java:

package my;
 
public class Calculator {
 
    public int plus(int a, int b) {
        return a + b;
    }
 
    public int times(int a, int b) {
        return a * b;
    }
 
    public int divide(int a, int b) {
        if (b == 0) {
            throw new RuntimeException("Division by zero attempted!");
        }
        return a / b;
    }
 
}

Instead of writing the CalculatorTest class that you would expect in JUnit/TestNG and clones testing frameworks, you can write the following scenario:

import my.Calculator
 
scenario "calculator manipulation", {
 
    given "a calculator", {
        calc = new Calculator()
    }
 
    then "sum should work", {
        calc.plus(1, 2).shouldBe 3
        calc.plus(0, 2).shouldBe 2
    }
 
    and "times should work", {
        calc.times(1, 1).shouldBe 1
        calc.times(1, 0).shouldBe 0
        calc.times(2, 2).shouldBe 4
    }
 
    and "divide should work", {
        calc.divide(4, 2).shouldBe 2
        calc.divide(1, 1).shouldBe 1
    }
 
    and "dividing by zero should fail", {
        ensureThrows(RuntimeException) {
            calc.divide(10, 0)
        }
    }
 
}

Isn't that way easier to read? :-)

Finally, easyb is able to write various kind of reports in text files or XML such as this one:

 1 scenario executed successfully

  Story: calculator

    scenario calculator manipulation
      given a calculator
      then sum should work
      then times should work
      then divide should work
      then dividing by zero should fail

That's just too good ;-) What do you think?

Share this post:
  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • DZone
  • Live
  • Netvibes
  • StumbleUpon
  • Technorati
  • FriendFeed
  • Wikio
  • Twitter
  • Identi.ca
  • Reddit
  • RSS
  • Slashdot

Related posts:

  1. Some download stats…
  2. A few updates from the current roadmap
  3. A fresh breeze in the testing frameworks world
  4. MySQL insanity
  5. Subversion + Git: when to choose one or the other?

Comments (0) Trackbacks (0)

No comments yet.


Leave a comment


No trackbacks yet.

JPz'log is Digg proof thanks to caching by WP Super Cache