Jfokus 2012 | 13-15 February | Stockholm, Sweden

Going to a good conference usually makes me want to write and keep this notepad alive. Going to a very good – and Jfokus 2012 was a very good conference in my opinion – makes me do it.

Jfokus 2012

In the next couple of days (understand weeks) I will force myself to go through the notes I took during the last 3 days and come up with a couple of posts. I swear.

My schedule

Day 1

  • Spring into the Cloud by Josh Long & Chris Richardson
  • A Practical Introduction To Kanban by Marcus Hammarberg & Joakim Sundén

Day 2

  • Enterprise Java in 2012 and Beyond by Juergen Hoeller
  • Player Framework 2.0 by Peter Hilton
  • Maven vs Gradle, On your marks, get set, go! by Hardy Ferentschik
  • Unleash Your Domain by Greg Young
  • An Intro to Hadoop by Eva Andreasson
  • Application Security for Rich Internet Applications by John Wilander
  • The road to REST by Rickard Öberg

Day 3

  • 7 Things: How to make good teams great by Sven Peters
  • HTML5 with Play Scala, CoffeeScript and Jade by Matt Raible
  • Zero Downtime Continuous Deployment of Java Web Applications by Fabiane Bizinella Nardon
  • It Is Possible to Do Object-Oriented Programming in Java by Kevlin Henney
  • Scala in Action by Heiko Seeberger
  • We visualized, we saw, we changed by Leonard Axelsson
  • Cool Code by Kevlin Henney

behaviour driven unit test design in Java

Writing unit tests is not that hard but writing good unit tests which, when they fail, tell you exactly and verbosely why and where it did not go right is not as trivial as it seems.

Let’s say we have a class that represents a square. The only operations we can perform on a square are: getting its surface and doubling the side length.

public class Square {

  private int sideLength;

  public Square(int sideLength) {
    this.sideLength = sideLength;
  }

  public int getSurface() {

    return this.sideLength * this.sideLength;

  }

  public void doubleSideLength() {

    this.sideLength = this.sideLength * 2;

  }

}

To test that piece of code, we could probably write a unit test that will look like:

public class SquareTest {

  public void testSurfaceWhenSideLengthIsDoubled() {

    Square a = new Square(5);
    Square b = a; b.doubleSideLength();

    assertEquals(a.getSurface(), b.getSurface() / 4);

  }

}

From a programmer’s point of view the code above perfectly makes sense but – even if the example is rather simple – it does not really tells us what scenario we want to test here. If this piece code would be read by a product manager, she would probably not understand it (oh well, in that case she probably will but unit tests can be a lot more complex than that).

What about making the code a little bit clearer and telling us a story? There are frameworks for that you could argue (like JBehave) but do we need a framework – and all the configuration, XML files that come with – for everything really? What if we just rewrite the test class so it is more narrative?

public class SquareTestEnhanced {

  private Square square;

  public void test_side_length_x_2_gives_surface_x_4() {

    when_doubling_is_done_on(new Square(5));
    then_surface_must_be_multiplied_by_4();

  }

  private void when_doubling_is_done_on(Square s) {

    this.square = s;

  }

  private void then_surface_must_be_multiplied_by_4() {

    Square s = this.square; s.doubleSideLength();
    assertEquals(this.square.getSurface() * 4, s.getSurface());

  }

}

Isn’t it enough? Can my product manager understand what we are testing between lines 5 and 10?

Install Sun Java JDK 5 on Ubuntu 10.04 (Lucid Lynx)

Add the partner repository

sudo add-apt-repository "deb http://ir.archive.ubuntu.com/ubuntu jaunty-updates main multiverse"

Resynchronize the package index files

sudo apt-get update

Install Sun JDK 5

sudo apt-get install sun-java5-jdk

Proudly powered by WordPress
Theme: Esquire by Matthew Buchanan.