Exercises


Exercises appear at the end of lesson. Many of the exercises have you build pieces of an application that plays the game of chess. If you are unfamiliar with the rules of chess, you can read them at http://www.chessvariants.com/d.chess/chess.html.

  1. As with Student, you will start simply by creating a class to represent a pawn. First, create an empty test class named PawnTest. Run JUnit against PawnTest and observe that it fails, since you haven't written any test methods yet.

  2. Create a test method named testCreate. Ensure that you follow the correct syntax for declaring test methods.

  3. Add code to testCreate that instantiates a Pawn object. Ensure that you receive a compile failure due to the nonexistent class. Create the Pawn class and demonstrate a clean compile.

  4. Assign the instantiated Pawn object to a local variable. Ask the pawn for its color. Code a JUnit assertion to show that the default color of a pawn is represented by the string "white". Demonstrate test failure, then add code to Pawn to make the test pass.

  5. In testCreate, create a second pawn, passing the color "black" to its constructor. Assert that the color of this second pawn is "black". Show the test failure, then make the test pass. Note: Eliminate the default constructorrequire that clients creating Pawn objects pass in the color. The change will impact the code you wrote for Exercise #4.

  6. In testCreate, create constants for the Strings "white" and "black". Make sure you rerun your tests.



Agile Java. Crafting Code with Test-Driven Development
Agile Javaв„ў: Crafting Code with Test-Driven Development
ISBN: 0131482394
EAN: 2147483647
Year: 2003
Pages: 391
Authors: Jeff Langr

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net