|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object junit.framework.Assert junit.framework.TestCase kareltherobot.KJRTest
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.* (if you want to use the other methods from JUnit). 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 UrRobot 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 UrRobot(...) 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. You DO need to build it each time however.
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.
Nested Class Summary |
Nested classes inherited from class kareltherobot.Directions |
Directions.Direction |
Field Summary |
Fields inherited from interface kareltherobot.Directions |
East, EastVal, infinity, North, NorthVal, South, SouthVal, West, WestVal |
Constructor Summary | |
KJRTest(java.lang.String name)
Create a new Karel J Robot test case with the given name. |
Method Summary | |
void |
assertAt(UrRobot robot,
int street,
int avenue)
Fail if the robot is not at a given intersection. |
void |
assertBeepersAt(int street,
int avenue)
Assert that a given corner has at least one beeper. |
void |
assertBeepersAt(int street,
int avenue,
int n)
Assert that a given corner has exactly n beepers |
void |
assertBeepersInBeeperBag(UrRobot robot)
Fail if the beeper bag of the robot is empty. |
void |
assertBeepersInWorld()
Assert that there is at least one beeper in the world. |
void |
assertBeepersInWorld(int n)
Fail unless there are exactly a certain number of beepers in the world |
void |
assertFacingEast(UrRobot robot)
Fail if the robot is not facing East. |
void |
assertFacingNorth(UrRobot robot)
Fail if the robot is not facing North. |
void |
assertFacingSouth(UrRobot robot)
Fail if the robot is not facing South. |
void |
assertFacingWest(UrRobot robot)
Fail if the robot is not facing West. |
void |
assertFrontIsBlocked(UrRobot robot)
Fail if there is no wall in front of the robot. |
void |
assertFrontIsClear(UrRobot robot)
Fail if there is a wall in front of the robot. |
void |
assertHasNeighbor(UrRobot robot)
Fail if the robot has no neighbors. |
void |
assertHasNoNeighbor(UrRobot robot)
Fail if the robot has any neighbors. |
void |
assertNextToABeeper(UrRobot robot)
Fail if the robot is on a corner with no beepers. |
void |
assertNoBeepersInBeeperBag(UrRobot robot)
Fail if the robot has any beepers in its beeper bag. |
void |
assertNotAt(UrRobot robot,
int street,
int avenue)
Fail if the robot is at a given intersection. |
void |
assertNotFacingEast(UrRobot robot)
Fail if the robot is facing East. |
void |
assertNotFacingNorth(UrRobot robot)
Fail if the robot is facing North. |
void |
assertNotFacingSouth(UrRobot robot)
Fail if the robot is facing South. |
void |
assertNotFacingWest(UrRobot robot)
Fail if the robot is facing West. |
void |
assertNotNextToABeeper(UrRobot robot)
Fail if the robot is on a corner with one or more beepers. |
void |
assertNotOnAvenue(UrRobot robot,
int avenue)
Fail if the robot is on a given avenue. |
void |
assertNotOnStreet(UrRobot robot,
int street)
Fail if the robot is on a given street. |
void |
assertNotRunning(UrRobot robot)
Fail if the robot not has turned off. |
void |
assertOnAvenue(UrRobot robot,
int avenue)
Fail if the robot is not on a given avenue. |
void |
assertOnStreet(UrRobot robot,
int street)
Fail if the robot is not on a given street. |
void |
assertRobotsAt(int street,
int avenue)
Assert that a given corner has at least one robot. |
void |
assertRobotsAt(int street,
int avenue,
int n)
Assert that a given corner has exactly n robots. |
void |
assertRobotsInWorld()
Assert that there is at least one robot in the world. |
void |
assertRobotsInWorld(int n)
Fail unless there are exactly a certain number of robots in the world |
void |
assertRunning(UrRobot robot)
Fail if the robot has turned off or experienced an error shutoff. |
Methods inherited from class junit.framework.TestCase |
countTestCases, getName, run, run, runBare, setName, toString |
Methods inherited from class junit.framework.Assert |
assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertFalse, assertFalse, assertNotNull, assertNotNull, assertNotSame, assertNotSame, assertNull, assertNull, assertSame, assertSame, assertTrue, assertTrue, fail, fail |
Methods inherited from class java.lang.Object |
equals, getClass, hashCode, notify, notifyAll, wait, wait, wait |
Constructor Detail |
public KJRTest(java.lang.String name)
name
- the name for this test.Method Detail |
public final void assertFacingNorth(UrRobot robot)
robot
- the robot of interest.public final void assertNotFacingNorth(UrRobot robot)
robot
- the robot of interest.public final void assertFacingEast(UrRobot robot)
robot
- the robot of interest.public final void assertNotFacingEast(UrRobot robot)
robot
- the robot of interest.public final void assertFacingSouth(UrRobot robot)
robot
- the robot of interest.public final void assertNotFacingSouth(UrRobot robot)
robot
- the robot of interest.public final void assertFacingWest(UrRobot robot)
robot
- the robot of interest.public final void assertNotFacingWest(UrRobot robot)
robot
- the robot of interest.public final void assertAt(UrRobot 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(UrRobot 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(UrRobot robot, int street)
robot
- the robot of interest.street
- the street you assert the robot is on.public final void assertNotOnStreet(UrRobot robot, int street)
robot
- the robot of interest.street
- the street you assert the robot is not on.public final void assertOnAvenue(UrRobot robot, int avenue)
robot
- the robot of interest.avenue
- the avenue you assert the robot is on.public final void assertNotOnAvenue(UrRobot robot, int avenue)
robot
- the robot of interest.avenue
- the avenue you assert the robot is not on.public final void assertHasNeighbor(UrRobot robot)
robot
- the robot of interest.public final void assertHasNoNeighbor(UrRobot robot)
robot
- the robot of interest.public final void assertNextToABeeper(UrRobot robot)
robot
- the robot of interest.public final void assertNotNextToABeeper(UrRobot robot)
robot
- the robot of interest.public final void assertBeepersInBeeperBag(UrRobot robot)
robot
- the robot of interest.public final void assertNoBeepersInBeeperBag(UrRobot robot)
robot
- the robot of interest.public final void assertFrontIsClear(UrRobot robot)
robot
- the robot of interest.public final void assertFrontIsBlocked(UrRobot robot)
robot
- the robot of interest.public final void assertRunning(UrRobot robot)
robot
- the robot of interest.public final void assertNotRunning(UrRobot robot)
robot
- the robot of interest.public final void assertBeepersInWorld(int n)
n
- the asserted number of beepers in the worldpublic final void assertBeepersInWorld()
public final void assertRobotsInWorld(int n)
n
- the asserted number of robots in the worldpublic final void assertRobotsInWorld()
public final void assertBeepersAt(int street, int avenue, int n)
street
- the street in questionavenue
- the avenue of the corner in questionn
- the asserted numbrer of beeperspublic final void assertBeepersAt(int street, int avenue)
street
- the street in questionavenue
- the avenue of the corner in questionpublic final void assertRobotsAt(int street, int avenue, int n)
street
- the street in questionavenue
- the avenue of the corner in questionn
- the asserted number of robots on this cornerpublic final void assertRobotsAt(int street, int avenue)
street
- the street in questionavenue
- the avenue of the corner in question
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |