kareltherobot
Class KJRTest

public class KJRTest
extends TestCase
implements Directions

Title: KJRTest

Description: Test harness for JUnit testing of Karel J. Robot programs.

Copyright: Copyright (c) 2003, Joseph Bergin

To use this harness, create a new subclass of KJRTest. The class should be in package kareltherobot AND should import junit.framework.*. The class needs a constructor with a String parameter, just as this class has. Declare any variables that you wish to use in the test as fields of this class, but do not initialize them in the declarations. For example, declare some ur_Robot variables.

Write a setUp method with no parameters returning void, in which you give values to all of the variables that you want to use elsewhere in your tests. If you have declared some robots as fields of your new class, then initialize these fields with new ur_Robot(...) or whatever.

Then write a set of public void methods whose names begin with "test", such as "testMoving", or "testTurning". Beginning with the word "test" is important to the infrastructure. Within these methods you can send messages to your robots and can call inherited methods of this class. Assert what you believe should be true of the state of your robots at any point. You can also call methods inherited here from the JUnit framework. Note that when an assertion "succeeds" nothing happens, but when it fails, an error is indicated to the test framework.

Finally, give your class a main with one statement:

      junit.swingui.TestRunner.run(UnitTest.class);
where you replace "UnitTest" with the name of your class. If you don't have access to the swing libraries (Mac OS 9) you can say:
    junit.awtui.TestRunner.run(UnitTest.class); 
instead. See UnitTest.java for a sample of such a class.

This new class should be the "main" class in your project. When you run the project, you will get a GUI with a run button. Pushing the run button will execute all of the "test" methods in your class, independently of the others but with setUp executed just prior to the execution of each. If none of the assertions fail then you will see a green bar. If any fail, you will see a red bar and be given some information about the failure, including which assertion failed and its line number.

NOTE that once the gui is running, you can change your code and rebuild it without terminating the gui. If the "Reload classes for every run" box is checked, the new code will be exercised when you hit the run button, so you don't need to "run" the project again.

For more advanced uses of this framework, read the documentation for TestSuite in the JUnit docs. You can build up a single main test from lots of smaller test classes to test parts of your application separately from others, but maintain the ability to run all the tests from all the classes at once.

Version:
1.0
Author:
Joseph Bergin
Constructor Detail

KJRTest

public KJRTest(String name)
Create a new Karel J Robot test case with the given name. This is called by the test infrastructure, not by the programmer. It is required.
Parameters:
name - the name for this test.

Method Detail

assertFacingNorth

public final void assertFacingNorth(ur_Robot robot)
Fail if the robot is not facing North. No-op if the robot IS facing North.
Parameters:
robot - the robot of interest.

assertNotFacingNorth

public final void assertNotFacingNorth(ur_Robot robot)
Fail if the robot is facing North. No-op if the robot IS NOT facing North.
Parameters:
robot - the robot of interest.

assertFacingEast

public final void assertFacingEast(ur_Robot robot)
Fail if the robot is not facing East. No-op if the robot IS facing East.
Parameters:
robot - the robot of interest.

assertNotFacingEast

public final void assertNotFacingEast(ur_Robot robot)
Fail if the robot is facing East. No-op if the robot IS NOT facing East.
Parameters:
robot - the robot of interest.

assertFacingSouth

public final void assertFacingSouth(ur_Robot robot)
Fail if the robot is not facing South. No-op if the robot IS facing South.
Parameters:
robot - the robot of interest.

assertNotFacingSouth

public final void assertNotFacingSouth(ur_Robot robot)
Fail if the robot is facing South. No-op if the robot IS NOT facing South.
Parameters:
robot - the robot of interest.

assertFacingWest

public final void assertFacingWest(ur_Robot robot)
Fail if the robot is not facing West. No-op if the robot IS facing West.
Parameters:
robot - the robot of interest.

assertNotFacingWest

public final void assertNotFacingWest(ur_Robot robot)
Fail if the robot is facing West. No-op if the robot IS NOT facing West.
Parameters:
robot - the robot of interest.

assertAt

public final void assertAt(ur_Robot robot,
                           int street,
                           int avenue)
Fail if the robot is not at a given intersection. No-op otherwise.
Parameters:
robot - the robot of interest
street - the street you assert the robot is on.
avenue - the avenue you assert the robot is on.

assertNotAt

public final void assertNotAt(ur_Robot robot,
                              int street,
                              int avenue)
Fail if the robot is at a given intersection. No-op otherwise.
Parameters:
robot - the robot of interest
street - the street of the corner you assert the robot is on.
avenue - the avenue of the corner you assert the robot is on.

assertOnStreet

public final void assertOnStreet(ur_Robot robot,
                                 int street)
Fail if the robot is not on a given street. No-op otherwise.
Parameters:
robot - the robot of interest.
street - the street you assert the robot is on.

assertNotOnStreet

public final void assertNotOnStreet(ur_Robot robot,
                                    int street)
Fail if the robot is on a given street. No-op otherwise.
Parameters:
robot - the robot of interest.
street - the street you assert the robot is not on.

assertOnAvenue

public final void assertOnAvenue(ur_Robot robot,
                                 int avenue)
Fail if the robot is not on a given avenue. No-op otherwise.
Parameters:
robot - the robot of interest.
avenue - the avenue you assert the robot is on.

assertNotOnAvenue

public final void assertNotOnAvenue(ur_Robot robot,
                                    int avenue)
Fail if the robot is on a given avenue. No-op otherwise.
Parameters:
robot - the robot of interest.
avenue - the avenue you assert the robot is not on.

assertHasNeighbor

public final void assertHasNeighbor(ur_Robot robot)
Fail if the robot has no neighbors. No-op otherwise.
Parameters:
robot - the robot of interest.

assertHasNoNeighbor

public final void assertHasNoNeighbor(ur_Robot robot)
Fail if the robot has any neighbors. No-op otherwise.
Parameters:
robot - the robot of interest.

assertNextToABeeper

public final void assertNextToABeeper(ur_Robot robot)
Fail if the robot is on a corner with no beepers. No-op otherwise.
Parameters:
robot - the robot of interest.

assertNotNextToABeeper

public final void assertNotNextToABeeper(ur_Robot robot)
Fail if the robot is on a corner with one or more beepers. No-op otherwise.
Parameters:
robot - the robot of interest.

assertBeepersInBeeperBag

public final void assertBeepersInBeeperBag(ur_Robot robot)
Fail if the beeper bag of the robot is empty. No-op otherwise.
Parameters:
robot - the robot of interest.

assertNoBeepersInBeeperBag

public final void assertNoBeepersInBeeperBag(ur_Robot robot)
Fail if the robot has any beepers in its beeper bag. No-op otherwise.
Parameters:
robot - the robot of interest.

assertFrontIsClear

public final void assertFrontIsClear(ur_Robot robot)
Fail if there is a wall in front of the robot. No-op otherwise.
Parameters:
robot - the robot of interest.

assertFrontIsBlocked

public final void assertFrontIsBlocked(ur_Robot robot)
Fail if there is no wall in front of the robot. No-op otherwise.
Parameters:
robot - the robot of interest.

assertRunning

public final void assertRunning(ur_Robot robot)
Fail if the robot has turned off or experienced an error shutoff. No-op otherwise.
Parameters:
robot - the robot of interest.

assertNotRunning

public final void assertNotRunning(ur_Robot robot)
Fail if the robot not has turned off. No-op otherwise. Note that an error shutoff is considered a turn off.
Parameters:
robot - the robot of interest.