XP Home

Test the Warmer Switch Now


The next unit test will test the warmer switch. We refactored our unit tests so now this is trivial.  

package simulator.r14.unittest;

class TestWarmer extends SwitchTest
{public void runTest()

public void warmerOff()

public void warmerOn()

We always test our test by creating stubs, compiling and running. Of course it fails. Now let's add some code to make it pass. We can add three methods checkWarmerSwitch(), turnOnWarmer() and turnOffWarmer(). Then change our run() method to call them.

package simulator.r15;

public class Simulator extends Thread implements Switches
{SimulationInterface gui;
private boolean boilerIsOn = false, reliefValveIsOn = false, warmerIsOn = false;

public Simulator (SimulationInterface aGUI)
gui = aGUI;}

public void run()
{while (true)

private void checkBoilerSwitch()
{if (wasJustSwitchedOn(BoilerSwitch, boilerIsOn)) turnOnBoiler();
if (wasJustSwitchedOff(BoilerSwitch, boilerIsOn)) turnOffBoiler();}

private void checkReliefValveSwitch()
{if (wasJustSwitchedOn(ReliefValveSwitch, reliefValveIsOn)) turnOnReliefValve();
if (wasJustSwitchedOff(ReliefValveSwitch, reliefValveIsOn)) turnOffReliefValve();}

private void checkWarmerSwitch()
{if (wasJustSwitchedOn(WarmerSwitch, warmerIsOn)) turnOnWarmer();
if (wasJustSwitchedOff(WarmerSwitch, warmerIsOn)) turnOffWarmer();}

private void turnOnBoiler()
boilerIsOn = true;}

private void turnOffBoiler()
boilerIsOn = false;}

private void turnOnReliefValve()
reliefValveIsOn = true;}

private void turnOffReliefValve()
reliefValveIsOn = false;}

private void turnOnWarmer()
warmerIsOn = true;}

private void turnOffWarmer()
warmerIsOn = false;}

private boolean wasJustSwitchedOn(int aSwitch, boolean isOnNow)
{return isSwitchedOn(aSwitch) && !isOnNow;}

private boolean wasJustSwitchedOff(int aSwitch, boolean isOnNow)
{return isSwitchedOff(aSwitch) && isOnNow;}

private boolean isSwitchedOff(int aSwitch)
{return (PIA.register & aSwitch) == 0;}

private boolean isSwitchedOn(int aSwitch)
{return !isSwitchedOff(aSwitch);}

private void sleepOneTenthSecond()
catch (InterruptedException exception)
We now run the unit tests again and they pass. Something is wrong with this code. It is getting way to large and hard to understand and there are lots of methods that look alike except for a couple name changes. I think it is time for a big refactoring. Spike Solution

ExtremeProgramming.org home | A Spike Solution | Refactor Again |

Copyright 1999 by Don Wells.