Making It Stick  (A Developers Implementation Plan)

Generate ‘real experience’

I first stumbled across the concept of the ‘learning test’ — from the book Clean Code 4. For those unfamiliar with this term, it’s an excellent approach for developers to drill down into detail, made even easier as we’re in an age of ‘configuration as code’. Simply put, rather than reading the documentation for software, use an available testing framework to **prove** what the documentation says.

This was exactly what I did — so for example when I read through a tutorial that said that a Java DateFormat worked in a certain way, I created a TestNG test that would prove (or otherwise) what I was reading.

Tutorial Material

Example: I wanted to see how the Locale and Format arguments worked when provided to DateFormat.getInstance()

Flashcard

Q: How would I retrieve a String of “Jan-01–1970” regardless of where my code was running?
Hint: This is the format used by the UK.

A: I would use the DateFormat .getInstance() Method and pass two arguments
The style would be DateFormat.MEDIUM (int)
The Locale would be Locale.UK (Enum)

Learning Test Examples

@Test
public void whenLocaleUkAndDateFormatIsMediumThenThreeLetterMonthAndDay() throws Exception{
    DateFormat dateFormat = DateFormat.getDateInstance(DateFormat.MEDIUM,Locale.UK);
    assertThat(dateFormat.format(EPOCH_DATE),is("01-Jan-1970"));
}

Example: I wanted to prove to myself that the DateFormat class would use a MEDIUM date format and the Locale of my machine when I provided zero args to the DateFormat.getInstance() factory method

Flashcard

Q: What would happen if you didn’t pass any arguments, to getInstance()?

A: You would get the default DateFormat style of MEDIUM and the Locale of wherever you were running the code.

@Test
public void whenLocaleDefaultAndDateFormatDefaultThenThreeLetterMonthAndDay() throws Exception{
    DateFormat dateFormat = DateFormat.getDateInstance();
    assertThat(dateFormat.format(EPOCH_DATE),is("01-Jan-1970"));
}

This was vital. By running through a series of tests, and seeming them fail, I was able to see that my existing understanding of API’s was wrong in some cases. I could see a test fail, find out from the official docs, or someone’s article and have a test I could run, at will to see the impact.

In some cases, these ‘lower level’ findings were important enough for me to create additional flashcards for, to remind me of something I wasn’t expecting.

Next we’ll look at how you can get use out these learning tests even after the initial learning has been done.