Hacking on GraphHopper - a Java road routing engine. Peter has posted 62 posts at DZone. You can read more from them at their website. View Full User Profile

Test-Driven Teaching

01.04.2010
| 10898 views |
  • submit to reddit

Sections Overview

0. This overview article with a ...

1. ... Hello World example

2. Initial Box Game

3. Object Oriented Programming – Refactoring!

4. User Interface – Swing!

5. More Game Features

The idea of test-driven development (TDD) is not a new one and also the idea of test-driven teaching is not new (see also at springer or here).

The main idea is to write working code examples based on TDD for beginners. The students should then be able to code (unit) tests and to read them from the very beginning. Most people learned TDD some years after learning ‘programming itself’, although I think TDD is already part of every coding. Most programmers ‘code’ + ‘test’ + ‘bug fix’ + ‘test’ + etc. But with TDD you have the benefit that your tests can be executed later automatically and very fast, which ensures that early requirements are valid

  • even if the same programmer forgot them or
  • if several programmers working on the same project at the same time or also
  • if a programmer must resume the work of another one.

I won’t give a lot details of TDD here, but the main advantages of TDD are:

  • If applied correctly (=early!) you can often code faster with TDD
  • Later refactorings are much more faster and less error prone (!!)
  • Testable code is often ‘better’ code in terms of readability and re-useability
  • You indirectly document the functionality and the (unit) tests can be used as feature request or bug reports

The ‘real’ development cycle for TDD is outlined at wikipedia. My subjective suggestion to you my fellow reader is:

  1. think and initial code
  2. think …
  3. test a little (write test)
  4. code a little
  5. test passed?
    • yes => repeat with 2. (if all other tests passed too)
    • no  => find the mistake and repeat with 4. or 3.

Within all steps refactoring could be involved.

So, additionally to the explanation what different primitive types in Java are, there will unit tests like the following:

 

@Test
public void testLongAndInteger() {
// an important difference of a long value to an integer value is the range:
int expectedInt = 2147483647;
// L indicates a long value
long expectedLong = 9223372036854775807L;
assertEquals( expectedInt, Integer.MAX_VALUE);
assertEquals(expectedLong, Long.MAX_VALUE);

// here is the problem what happens to an integer variable which
// exceeds its domain: increasing the maximal value will cause the
// variable to be negative!
int intVal = Integer.MAX_VALUE + 1;
assertTrue(intVal < 0);
}

This simple tutorial will not replace a good old Java textbook e.g. the JavaInsel (German) or other, but could be used in parallel. It is a pity that even such big and great books do not teach such important hand toolings like TDD.

Other important learning steps should be involved while teaching a programming language:

  • Version control (subversion, bazaar, mercury, git, …)
  • IDE handling (run, debug, test, refactor, format, … )
  • Using external libraries or link to resource lists (e.g. this one)

So, how should we structure such a beginner course where test-driven development is involved?

As a beginner I would prefer a ‘real life’ project. We should develop an idea and implement this while teaching (with refactoring!). It should be an idea which does not require a lot of set-up or external resources (such as an internet connection, web app etc). And it should be either useful (sth. like a xml conversion tool), a game or sth. that looks nice.

In this tutorial we will implement a simple game with Swing as the graphical user interface (GUI) to make it visually more appealing than an ordinary console output. The Swing coding chapters should and can be skipped at the first time of reading. Although user interface code can be tested (e.g. with FEST) they won’t, because of the lack of time of myself. The game itself should look like this but actually won’t (again lack of time).

If you would like to hire teaching courses here in Germany: drop me an email via peathal ät yahoo.de

Let's get started now with the first part

Published at DZone with permission of its author, Peter Karussell.

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)

Comments

Peter Karussell replied on Mon, 2010/01/04 - 9:01am

Please look at the original article series for an updated version.

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.