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

  • Scala Programming Projects
  • Mikael Valot Nicolas Jorand
  • 279字
  • 2021-07-23 16:25:19

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.

主站蜘蛛池模板: 岳阳县| 深水埗区| 泽州县| 治多县| 如皋市| 澄城县| 深水埗区| 鄂伦春自治旗| 鄂托克前旗| 阜康市| 开原市| 自贡市| 伊通| 丹东市| 衡东县| 桂平市| 城固县| 南木林县| 阆中市| 慈利县| 彰武县| 莎车县| 宁远县| 鞍山市| 峨山| 常熟市| 曲靖市| 定州市| 文登市| 东明县| 富川| 永州市| 东光县| 广东省| 共和县| 苍南县| 濮阳市| 夹江县| 德昌县| 纳雍县| 澄迈县|