Java Test-Driven Development Lab Exercise

1. Account balance exercise

Using TDD, complete the following exercise.

Create a new Account class, and a corresponding AccountTest class, using the process we’ve previously demonstrated in lecture.

Write the following tests for the Account class:

For each of the test cases:

  1. Implement the test for that test case. Uncomment it and add a test code inside it.
  2. Fix compile errors.
  3. Watch the test fail.
  4. Write now code that you expect to make the test pass.
  5. Watch the test pass. If any of your tests fail, you should repeat step #4.
  6. Commit your changes and go back to Step #1 for the next test case.
Given When Then
I have $100 in my account I deposit $50 I see that my account contains $150
I have $100 in my account I withdraw $50 I see that my account contains $50
I have $50 in my account I withdraw $100 I see that my account contains $50

2. Factorial exercise

Using TDD, complete the following exercise.

Create a new Factorial class, and a corresponding FactorialTest class, using the process we’ve previously demonstrated in lecture.

Write the following tests for the Factorial class:

For each of the test cases:

  1. Implement the test for that test case. Uncomment it and add a test code inside it.
  2. Fix compile errors.
  3. Watch the test fail.
  4. Write now code that you expect to make the test pass.
  5. Watch the test pass. If any of your tests fail, you should repeat step #4.
  6. Commit your changes and go back to Step #1 for the next test case.