Platinum Partner
java,netbeans,testing,test-driven development,java tutorial

Test-Driven Teaching

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

The first part of this tutorial for beginners is the ordinary ‘Hello World’ example which won’t be tested. Hey why not? Because this simple example is only used to check if the set-up works.

What you will learn in this section:

  • Set-up of a simple Java project
  • IDE: run the application
  • Methods with parameters
  • for loop

The reader will learn how to set-up the example in his integrated development environment (IDE) of choice, build and run it. He will see that the ‘Hello World’ will be printed in the console output window of the IDE.

I will explain the set-up for NetBeans only. But there are other free and very powerful IDEs as well. E.g. Eclipse and IntelliJ Idea can be used as good alternatives.

As the very first step download and install the Java Development Kit (JDK) from Sun (not Java Runtime Environment = JRE). Then get the latest NetBeans here where the ‘Java SE’ package is sufficient.

Get the example as zip file here and extract it into your local folder using NetBeans:

Open the favourite window under Window->Favourites. Right click into this window and add the download folder (with the zip file) to favourites:

Expand the zip file (left triangle) and copy the folder via right click:

Then paste it where you like. Open the project via right click->Open Project ‘TestDrivenTeaching1′ or open it via File->Open Project…

All the code is free software and stands under the GPL v3.0. All sources are located under the src folder; all subfolders of this folder are equivalent to Java packages and are used to better organize your code. You will see the logical ‘Java package’-view on this project within the ‘Projects Window’ and the ‘ordinary folder’-view in the ‘Files Window’:

So look into the package de.tdtut.section1 to open the HelloWord class with double click:

Then click on ‘Run File’ or press SHIFT F6 within this HelloWorld file and you will see in the ‘Output Window’ sth. like:

run:

Hello World

0

BUILD SUCCESSFUL
(The first and last statement are generated from NetBeans and should be ignored!)

Under the hood the character-band “Hello World” called String will be passed as the single parameter to the method println (‘print line to standard output’).

Every standalone Java application can be started outside of NetBeans e.g. from command line. Go to the folder of the project and type:

java -cp dist/TestDrivenTeaching.jar de.tdtut.section1.HelloWord

As output you should see ‘Hello World’ only. The last argument must be a class with the main method:

public static void main(String[] args)

If you append some arguments e.g. via:

java -cp dist/TestDrivenTeaching.jar de.tdtut.section1.HelloWord arg1 arg2

You will see

Hello World
2
arg1
arg2

This is because of the for-loop, which looks into the list of strings and picks one after another.

for (String arg : args) {
System.out.println(arg);
}

This list is called array and was initialized from the command line arguments and has a static lenght, which cannot be changed afterwards. If you want to access one element of an array you can do this via args[0] and args[1]. Be sure that the number (called index) starts from 0 and goes only until args.length – 1. So if you want to have full control of the iteration use this version with an explicit counter of type int:

for (int i = 0; i < args.length; i++) {
System.out.println(args[i]);
}

The counter i is declared (and visible) only within the block of the for-loop. With the line

int i = 0;
i was declared and initialized with 0. After each step i will be incremented by one. The loop will execute its block only if the counter i is smaller than args.length.

Questions

If the last sentences were a bit overwhelming for your head: no problem! Just try a bit around or go ahead to the next section!

E.g. you could declare some other variables and print them or try to iterate in inverse order through the array args. The most important step to learn programming or a new programming language is to think what you want and try to implement this.

From http://karussell.wordpress.com

{{ tag }}, {{tag}},

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}
{{ parent.authors[0].realName || parent.author}}

{{ parent.authors[0].tagline || parent.tagline }}

{{ parent.views }} ViewsClicks
Tweet

{{parent.nComments}}