XP Home

Unit Test the Boiler Turning Off

 

The next thing we can test is turning the boiler off. In order to test the boiler turning off we must first turn on the boiler then shut it back off. So let's extend the TestBoilerOff test to also test for the boiler coming back on. We will need to rename it to be TestBoiler.  

package simulator.r8b.unittest;

import unittest.framework.*;
import simulator.r8b.*;

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

public void setUp()
{onMessageSent = 0;
offMessageSent = 0;
PIA.register = 0x0000;
PIA.setInputs(0x003F);
startSimulator();}

public void runTest()
{testBoilerOn();
testBoilerOff();}

private void testBoilerOn()
{PIA.write(0x1000);
pauseOneQuarterSecond();
should(onMessageSent == 1, "Got boilerOn " + onMessageSent + " instead of once");
should(offMessageSent == 0, "Got a different message");}

private void testBoilerOff()
{PIA.write(0x0000);
pauseOneQuarterSecond();
should(onMessageSent == 1, "Got a different message");
should(offMessageSent == 1, "Got boilerOff " + offMessageSent + " instead of once");}

public void tearDown()
{stopSimulator();}

public void boilerOff()
{offMessageSent++;}

public void boilerOn()
{onMessageSent++;}

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

private void pauseOneQuarterSecond()
{try
{Thread.sleep(250);}
catch (InterruptedException exception)
{};}

private void stopSimulator()
{simulation.stop();
simulation = null;};}
We can change the test suite, create stubs for the methods we expect to be calling, , compile them, and try to run it. As we can see we are not getting our boiler off message as we expected. So now let's create that code to do that. This time I am just going to show the changes we will make to the Simulator class. We will also update the SimulationInterface to include boilerOff().

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

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

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

private void turnOffBoiler()
{gui.boilerOff();
boilerIsOn = false;}
Now we run the unit test and it passes. What now? That's right, another unit test! Spike Solution

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

Copyright 1999 by Don Wells.