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:
shouldIncreaseMyBalanceWhenIDepositMoney- This test should create anAccountobject, deposit $50, and then check the balance.shouldDecreaseMyBalanceWhenIWithdrawMoney- This test should create anAccountobject, deposit $100, withdraw $50, and then check the balance.shouldNotDecreaseMyBalanceWhenIWithdrawMoneyAndDoNotHaveEnoughToCoverTheWithdrawal- This test should create anAccountobject, deposit $50, withdraw $100, and then check the balance.
For each of the test cases:
- Implement the test for that test case. Uncomment it and add a test code inside it.
- Fix compile errors.
- Watch the test fail.
- Write now code that you expect to make the test pass.
- Watch the test pass. If any of your tests fail, you should repeat step #4.
- 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:
shouldReturnOneWhenNumberIsOne- This test should create aFactorialobject, compute the factorial of 1, and then check the result.shouldReturnTwoWhenNumberIsTwo- This test should create aFactorialobject, compute the factorial of 2, and then check the result.shouldReturnOneWhenNumberIsZero- This test should create aFactorialobject, compute the factorial of 0, and then check the result.shouldReturnSixWhenNumberIsThree- This test should create aFactorialobject, compute the factorial of 3, and then check the result.shouldThrowIllegalArgumentExceptionWhenNumberIsNegative- This test should create aFactorialobject, compute the factorial of -1, and then check that anIllegalArgumentExceptionis thrown.
For each of the test cases:
- Implement the test for that test case. Uncomment it and add a test code inside it.
- Fix compile errors.
- Watch the test fail.
- Write now code that you expect to make the test pass.
- Watch the test pass. If any of your tests fail, you should repeat step #4.
- Commit your changes and go back to Step #1 for the next test case.