Test-Driven Teaching: Initial Box Game

DZone 's Guide to

Test-Driven Teaching: Initial Box Game

· Java Zone ·
Free Resource

Now that you are a bit comfortable with your IDE and Java itself we will code the initial lines of the game in this tutorial.

What you will learn in this section:

  • Get the source code with subversion: ‘check out’
  • Variables of different type
  • Java methods with parameters
  • Variables with different visibility
  • Creating junit tests and run them
  • IDE:
    • debug the application
    • autocompletion
  • if-then-else constructs

Get The Project

This project won’t be available as zip file. You need to install a subversion client (e.g. TortoiseSVN for windows and KDESvn for linux) or use subversion in your IDE. For NetBeans go to the Team menu->Checkout…

Then type in the following url:


No login or password is required only specify the ‘Local Folder’ to match your needs. Open the project if NetBeans asks you. For some screenshots look here.

Right click the project->and click Test. The unit test window should pop up and all tests should pass (green).

Get The Knowledge

Open the class InitialBlockGame which is located under the package called de.tdtut.section2. Look around a bit. You might have already noticed in the first part that there are special characters which looks grey in NetBeans: the comments which do not effect the program execution.


int myVariable;

This is an integer variable, which can hold only integer values, such as 1,2, -3 etc. the maximal value is stored under Integer.MAX_VALUE. If you want to initialize a variable do:

myVariable = 3;

You can do declaration and initialization at the same time:

int myVariable = 3;

Now it is important that you look into the test case shipped with the second project to get a better understanding of the various types and possible operations. To run the test right click into the file and click ‘Test File’ or press CTRL F6.

Hint for later usage: If you create an additional Java class within the package via right click on the package->New->Java Class…, then you can easily generate a new test file for it: CTRL SHIFT U


You now know variables and comments but also note that there are blocks like the for-loop in the previous post defined by curly brackets:

public class InitialBlockGame {

and e.g. the method definition

public static void start() {

The first block is the definition of a class called InitialBlockGame and the second block is a method called start without a return type, so: void start(). There are also methods like move which requires method parameters.

Variables – Visibility/Access

If you declare a variable it will be accessible only from within that block and also sub-blocks. The following code fragment should illustrate this:

public class InitialBlockGame {
public int x;
public void start() {
int test;
// here we can use the test and x variables

public void move(int dx, int dy) {
// here we can use the x, dx, and dy variables
// but not the test variable declared within the start() method

We cannot easily create a test case for this, because e.g. the usage of test within the move method wouldn’t compile at all! But in scripting language like Ruby or Groovy this could be done.

Another important approach to manage the access of variables or even methods via the public, protected and private keywords will be explained in the next section.


To debug a Java class right click into the file (e.g. InitialBlockGame) and click ‘Debug File’ or CTRL SHIFT F5. But before you should set a breakpoint e.g. on the line

Random rand = new Random(123);

Then go through the code step by step via F8. Investigate the value of the variable lifeCounter through holding the mouse over it.

Another approach is to open the Watches window via Window->Debugging->Watches and add lifeCounter to it (right click->New Watch…)


Simply press CTRL SPACE e.g. after ‘InitialBlockGame.’ and all variables and methods will be listed:

This makes it very easy and fast to learn and explore unknown code (e.g. a library). This is the most powerful command an IDE provides you compared to a simple text editor.

Try to invoke this command (CTRL SPACE) within the class definition block or after you typed ‘int’ within a method to see what the IDE provides you to make development easier.


If you look into the move method you will see some if-statements e.g.:

if (x + dx >= 0 && x + dx < 10)
x = x + dx;

This means that if the change of move in x-direction called dx is too large it won’t be applied. The same is true for a negative dx.

Look into the test case testMove() to get a better understanding and play around a bit with the code.

From http://karussell.wordpress.com/


Opinions expressed by DZone contributors are their own.

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

{{ parent.tldr }}

{{ parent.urlSource.name }}