Writing a test for the decumulation phase
Now you know how much capital you can expect at your retirement date. It turns out you can reuse the same futureCapital function, to work out how much capital will be left for your heirs.
Add the following test in RetCalcSpec, underneath the previous unit test, and run it. It should pass:
"RetCalc.futureCapital" should {
"calculate how much savings will be left after having taken a pension
for n months" in {
val actual = RetCalc.futureCapital(
interestRate = 0.04/12, nbOfMonths = 40 * 12,
netIncome = 0, currentExpenses = 2000, initialCapital =
541267.1990)
val expected = 309867.53176
actual should ===(expected)
}
}
So, if you live for 40 years after your retirement date, spend the same amount every month, and don't have any other income, you will still have a significant capital left for your heirs. If the remaining capital was negative, that would have meant that you would have run out of money at some point during your retirement and it is an outcome we want to avoid.
Feel free to call the function from the Scala Console and try different values that would match more closely to your personal situation. Try different values for the interest rate and observe how you can end up with a negative capital if the rate is low.
Note that, in a production system, you would certainly add more unit tests to cover some other edge cases and make sure that the function will not crash. As we will cover error handling in Chapter 3, Handling Errors, we can assume that the test coverage of futureCapital is good enough for now.