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?
Related posts:
- Some download stats…
- A few updates from the current roadmap
- A fresh breeze in the testing frameworks world
- MySQL insanity
- Subversion + Git: when to choose one or the other?

