XP Home

Add Yet Another Unit Test
to the Simulator Class


Let's test the next switch. We choose the relief valve. First the test code. We notice (since we cut and pasted it) that this test is very similar to the boiler test. We have an opportunity for refactoring, but let's wait until we have two complete examples before we try to merge them together.

package simulator.r10.unittest;

import unittest.framework.*;
import simulator.r10.*;

class TestReliefValve extends Test implements SimulationInterface
{private int onMessageSent, offMessageSent, otherMessageSent;
private Thread simulation;

public void setUp()
{onMessageSent = 0;
offMessageSent = 0;
otherMessageSent = 0;
PIA.register = 0x0000;

public void runTest()

private void testReliefValveOn()
should(onMessageSent == 1, "Got reliefValveOn " + onMessageSent + " instead of once");
should(offMessageSent == 0, "Got a different message");}

private void testReliefValveOff()
should(onMessageSent == 1, "Got a different message");
should(offMessageSent == 1, "Got reliefValveOff " + offMessageSent + " instead of once");
should(otherMessageSent == 0, "Got some other message");}

public void tearDown()

public void boilerOff()

public void boilerOn()

public void reliefValveOff()

public void reliefValveOn()

private void startSimulator()
{simulation = new Simulator(this);

private void pauseOneQuarterSecond()
catch (InterruptedException exception)

private void stopSimulator()
simulation = null;};}
We add this to the test suite, and create stubs for the methods we expect to be calling, now compile it all, and try to run it. It fails. Let's make some changes to the simulator class. And add our new methods reliefValveOn() and reliefValveOff() to our SimulationInterface.

package simulator.r11;

public class Simulator extends Thread
{SimulationInterface gui;
boolean boilerIsOn = false;
boolean reliefValveIsOn = false;
static final int BoilerSwitch = 0x1000;
static final int ReliefValveSwitch = 0x2000;

public Simulator (SimulationInterface aGUI)
gui = aGUI;}

public void run()
{while (true)

private void checkBoilerSwitch()
{if (wasBoilerJustSwitchedOn()) turnOnBoiler();
if (wasBoilerJustSwitchedOff()) turnOffBoiler();}

private boolean wasBoilerJustSwitchedOn()
{return isBoilerSwitchedOn() && boilerIsOff();}

private boolean wasBoilerJustSwitchedOff()
{return isBoilerSwitchedOff() && boilerIsOn;}

private boolean isBoilerSwitchedOn()
{return !isBoilerSwitchedOff();}

private boolean isBoilerSwitchedOff()
{return (PIA.register & BoilerSwitch) == 0;}

private boolean boilerIsOff()
{return !boilerIsOn;}

private void turnOnBoiler()
boilerIsOn = true;}

private void turnOffBoiler()
boilerIsOn = false;}

private void checkReliefValveSwitch()
{if (wasReliefValveJustSwitchedOn()) turnOnReliefValve();
if (wasReliefValveJustSwitchedOff()) turnOffReliefValve();}

private boolean wasReliefValveJustSwitchedOn()
{return isReliefValveSwitchedOn() && reliefValveIsOff();}

private boolean wasReliefValveJustSwitchedOff()
{return isReliefValveSwitchedOff() && reliefValveIsOn;}

private boolean isReliefValveSwitchedOn()
{return !isReliefValveSwitchedOff();}

private boolean isReliefValveSwitchedOff()
{return (PIA.register & ReliefValveSwitch) == 0;}

private boolean reliefValveIsOff()
{return !reliefValveIsOn;}

private void turnOnReliefValve()
reliefValveIsOn = true;}

private void turnOffReliefValve()
reliefValveIsOn = false;}

private void sleepOneTenthSecond()
catch (InterruptedException exception)
Now we run the unit test and it passes. Before we can add our next unit test we need to spend some time refactoring the two tests we already have so that we can add the third simply. Spike Solution

ExtremeProgramming.org home | A Spike Solution | Next Unit Test |

Copyright 1999 by Don Wells.