CSCI 306
JUnit Exercise
Part II Due: Wednesday February 13
Part III Due: Tuesday February 19
Purpose:
- To understand unit testing
- To continue to work with Java GUI programming
- To be exposed to Java concepts: packages, enumerated types,
instanceof operator, equals vs ==, abstract class, file I/O, exception
handling, pass by
value (use of new inside a function/class that has a reference). In
Eclipse, notice Tasks view.
Preparation:
- Read Big Java 8.10 Unit Test Frameworks
- Download and unzip chess.zip from Blackboard - not in your
workspace
- Import the project into Eclipse. Choose File, Import,
Existing Projects Into Workspace
- To review the rules for the game of Chess, review the site: http://www.conservativebookstore.com/chess/setup.htm
Lesson
Part I: Understanding JUnit
- To use JUnit you will need to use the JUnit libraries. In
Eclipse, right-click on the Chess Project. Select
Properties. When the dialog box appears, select Java Build
Path. Then click the Add Library button. Select
JUnit. We'll experiment briefly with both JUnit 3 and JUnit 4, so
add both libraries (one at a time).
- Take a look at the Piece class. This is the abstract class
for all game Pieces. Notice that it has a method named
validMove. This method takes a Board (which contains the playing
grid) and an x/y grid coordinate as parameters and returns a status
that determine whether the move is valid. Notice the return
type. This is an enumerated type. It is declared in the
ChessPanel class. Take a look at that class now to see how it is
declared. NOTE: there is more that can be done with enumerated
types in Java, this is just a simple example.
- Now look at the Pawn class. Notice that it defines the
abstract method validMove. It will return
ChessPanel.MoveStatus.validMove if the proposed move is allowed, or
ChessPanel.MoveStatus.invalidMove if the proposed move is not allowed.
- Now look at the PiecesTest.java class. This is the unit
test for many of the pieces. Notice the first method is named testPawnFirstMove. This
function includes 3 methods that test possible first moves for a pawn,
two are valid (one takes the 2 steps which are only allowed on the
first move), one is not valid. Notice that the method calls the
Board constructor and then the Board method initialize to set up a Board
configuration. It then uses the Board method getPiece to access a Pawn to be used
for the test.
- To run the test, press the arrow beside the Run icon. Then
choose RunAs JUnit. That will run all of the tests, not
just the first one. NOTE: Normally there might only be one test
per method, but with the amount of setup required per test I have
decided to group the tests together. This works well when the
tests all succeed, not as well when tests are failing. We'll
discuss this in class!
- Now look at the testPawnCaptureMoves
method. Notice that this method loads a Board configuration
from a file. Notice the three specific tests that will be
run. To see what those tests are, you should load the same file
into the chess game. Double-click on ChessGame in the chess
package (notice the package statements at the top of the source
files). With it selected you can run the chess game. When
it starts, select Load and then enter "pawntest.txt" as the
filename. Then take a look at the JUnit tests. Do they make
sense?
- Now look at PiecesTestv4.
This program runs the same tests as testPawnFirstMove,
but it uses the JUnit 4 framework. Assuming you added both
libraries, you can now run PiecesTestv4 to see how it compares to the
JUnit 3 framework.
Part II: Writing your Own Methods
and
Tests
- First notice that there are no tests for the Knight. Create
a test file that can be used to test the Knight's moves. The
easiest way to do this is to uncomment the line:
return ChessPanel.MoveStatus.valid;
from the validMove method of
each of the
pieces. This will allow you to just move the pieces around on the
Board as you'd like, without going through an entire game sequence
(although you will still need to alternate players).
- Now save the Board to a file, maybe called knightTest.txt.
- Now create a set of tests in either piecesTest or piecesTestv4. Be sure to test
both valid and invalid moves.
- Next, notice that the Queen does not have a complete validMove method. Complete
that method for the Queen (take a look at the Chess reference above if
you need to remember how the Queen can move). Then create a Board
and a series of tests to ensure your Queen moves correctly.
HINT: The saved Board configurations can also be handy for debugging.
Part III: Completing the Game
There are several aspects of the game which have not yet been
coded. Adding those features is a design exercise.
- First, read the Pawn En Passant link on the rules website.
Although this rule is invoked infrequently, it would be good if our
game allowed it. With your partner, add that to the game.
Be sure to think about the design before you begin to code.
- Next, you will notice that the game status announces when a piece
has the King in check, but that's it. It doesn't detect
checkmate, or force the King to move, or announce when the game is won,
etc. How do you think the program should respond? Again
with your partner, decide how the game should respond and implement
that behavior.
- Code maintainability is an important issue. Take a look at
the comments throughout the code. Are they sufficient? Add
more where needed (but not on every line!). Also, take a look at
the canCaptureKing method. This code is ugly. Can anyone
come up with a more elegant approach?
- Feel free to make any other aesthetic changes you'd like (e.g.,
change colors, different prompts, etc.). Also, notify us of any
bugs - we'll use those as discussion items.
Part IV: Test Your Understanding - On Your Own
Remember that one goal for this exercise is to learn a variety of Java
concepts. To test your understanding, on your own or (preferably)
with your partner, answer the questions located here. Nothing to
turn in, this is just for your benefit.
Submission:
Part II: Zip your
project and submit and Blackboard. Submit just one copy per team
and be sure to include your partner's name. Your submission
should include:
- adequate tests for the Knight added to PiecesTest or
PiecesTestv4.
- a complete validMove method for the Queen.
- adequate tests for the Queen added to PiecesTest or PiecesTestv4.
Part III: Zip your final game
and submit on Blackboard. Submit just one copy per
team and be sure to include your partner's name in the Blackboard
comments. Your submission should include:
- implementation of Pawn En Passant
- implementation of some Checkmate or End of Game behavior.
Extra Credit (2 points): Find (and fix) a bug, suggest an improvement,
add another feature.