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.
Constructor Detail |
public KJRTest(String name)
name
- the name for this test.Method Detail |
public final void assertFacingNorth(ur_Robot robot)
robot
- the robot of interest.public final void assertNotFacingNorth(ur_Robot robot)
robot
- the robot of interest.public final void assertFacingEast(ur_Robot robot)
robot
- the robot of interest.public final void assertNotFacingEast(ur_Robot robot)
robot
- the robot of interest.public final void assertFacingSouth(ur_Robot robot)
robot
- the robot of interest.public final void assertNotFacingSouth(ur_Robot robot)
robot
- the robot of interest.public final void assertFacingWest(ur_Robot robot)
robot
- the robot of interest.public final void assertNotFacingWest(ur_Robot robot)
robot
- the robot of interest.public final void assertAt(ur_Robot robot, int street, int avenue)
robot
- the robot of intereststreet
- the street you assert the robot is on.avenue
- the avenue you assert the robot is on.public final void assertNotAt(ur_Robot robot, int street, int avenue)
robot
- the robot of intereststreet
- the street of the corner you assert the robot is on.avenue
- the avenue of the corner you assert the robot is on.public final void assertOnStreet(ur_Robot robot, int street)
robot
- the robot of interest.street
- the street you assert the robot is on.public final void assertNotOnStreet(ur_Robot robot, int street)
robot
- the robot of interest.street
- the street you assert the robot is not on.public final void assertOnAvenue(ur_Robot robot, int avenue)
robot
- the robot of interest.avenue
- the avenue you assert the robot is on.public final void assertNotOnAvenue(ur_Robot robot, int avenue)
robot
- the robot of interest.avenue
- the avenue you assert the robot is not on.public final void assertHasNeighbor(ur_Robot robot)
robot
- the robot of interest.public final void assertHasNoNeighbor(ur_Robot robot)
robot
- the robot of interest.public final void assertNextToABeeper(ur_Robot robot)
robot
- the robot of interest.public final void assertNotNextToABeeper(ur_Robot robot)
robot
- the robot of interest.public final void assertBeepersInBeeperBag(ur_Robot robot)
robot
- the robot of interest.public final void assertNoBeepersInBeeperBag(ur_Robot robot)
robot
- the robot of interest.public final void assertFrontIsClear(ur_Robot robot)
robot
- the robot of interest.public final void assertFrontIsBlocked(ur_Robot robot)
robot
- the robot of interest.public final void assertRunning(ur_Robot robot)
robot
- the robot of interest.public final void assertNotRunning(ur_Robot robot)
robot
- the robot of interest.