View on GitHub

COMP 491/492

Dickinson College Computer Science Senior Seminar

Test Driven Development

This activity is an introduction to Test Driven Development using Java and JUnit tests. The problem being solved is to create a Java class that can be used to represent a password.

Setup

Follow the steps below to get setup for this activity:

  1. Open a browser and log into your GitHub account.
  2. Click the link below:
  3. Click the Green “Create new codespace” button to create a Codespace using the starter project for the activity:

    Create new Codesapce

This will open VSCode in your browser, but it will take a few minutes to complete.

  1. When the files appear in the “EXPLORER” pane, click the PasswordTest.java file to open it.

  2. Wait for the dialog about sharing data with Redhat to appear in the lower right corner. Click Close or “Deny” this dialog dialog. This may take a few moments.

  3. Wait for the status bar at the bottom of the browser window to display:

    Java: Lightweight Mode

  4. Click the “Java: Lightweight Mode” area of the status bar.

  5. That area of the status bar will change a few times and then eventually it will display:

    Java: Ready

Once you see “Java: Ready” you are ready to go!

Running the Test

The provided PasswordTest.java file contains a single JUnit test that checks if an instance of the Password class can be created. This is a reasonable first test for a TTD approach to our problem of creating a Java class to represent a password.

Run this test by:

  1. Right click on the PasswordTest.java file in the “EXPLORER PANE”
  2. Choose “Run Tests” from the pop-up menu.
  3. When the tests run the “Test Runner for Java” panel should open in the lower right corner of the window.
    • If the “Test Runner for Java” panel doesn’t open, click the “Tests Results” tab that appears at the top of the Terminal panel.

Observe that the test has failed with an “Unresolved Compilation” error. This should not be too surprising since we haven’t yet created the Password class.

Making the Test Pass

To make the test pass you’ll need to create the Password class.

Create the Password class by:

  1. Right click in the empty are of the EXPLORER panel.
  2. Choose “New Java File” in the pop-up menu.
  3. Choose “Class…” from the pop-up sub-menu.
  4. Enter Password as the name for the new class.

Notice that a new Password.java file has been created and it contains the skeleton for the Password class. This should be sufficient for the test that was failing to now pass.

Run the test in PasswordTest.java again and observe that it now passes.

Next Steps

Now it’s time to do some Test Driven Development.

Consider the following requirements for the Password class:

Practice TDD to implement the Password class by following the six steps illustrated in the flowchart below, which was adapted from Martin Fowler’s short description of Test Driven Development:

Flowchart adapted from Martin Fowler's blog that illustrates the TDD process and the six steps described below.

  1. Create a list of tests that you will use.
  2. Pick a test to write.
  3. Write and run a JUnit test for that test.
  4. Write the code to implement the functionality being tested.
    • Remember, do the minimum amount of work you can to do get the test to pass.
      • If you think you need to do more, define a new test and add it to your list.
      • Then still do the minimum amount of work for the test you are working on.
    • Run the tests and debug until all tests including the new test pass.
  5. Do any refactoring to clean up and/or simplify the code.
    • Run the tests frequently during refactoring to ensure you have not broken anything.
  6. If your list of tests is not empty, goto 2.

Changing Requirements

If you have more time consider these additional requirements and then use TDD to implement some or all of them. Be sure to refactor your code as necessary after getting each new test to pass.


Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License All textual materials used in this course are licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License

GPL V3 or Later All executable code used in this course is licensed under the GNU General Public License Version 3 or later