官术网_书友最值得收藏!

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 3Handling Errors, we can assume that the test coverage of futureCapital is good enough for now.

主站蜘蛛池模板: 曲沃县| 太湖县| 武汉市| 曲周县| 福贡县| 武川县| 柏乡县| 韶山市| 闽侯县| 宣城市| 介休市| 宣化县| 察隅县| 霍州市| 宜川县| 建瓯市| 古交市| 丹巴县| 舟山市| 房产| 迁安市| 荣昌县| 泸水县| 孟津县| 周宁县| 新营市| 苗栗市| 竹溪县| 饶阳县| 弥勒县| 胶州市| 达日县| 怀安县| 潜山县| 库伦旗| 岳阳县| 焦作市| 尉犁县| 河东区| 苏尼特右旗| 青海省|