复利计算器单元测试
Posted 05卢琪
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了复利计算器单元测试相关的知识,希望对你有一定的参考价值。
针对复利计算器的单元测试程序
单元测试场景:
1.测试计算公式是否正确
2.测试用户输入非法字符
3.测试输入为空
4.测试输入字符串长度为0
详细源程序:https://github.com/425221830/CompoundInterestCalculator
单元测试代码
1 package pers.xisven.test; 2 3 import static org.junit.Assert.*; 4 5 import org.junit.Test; 6 7 import pers.xisven.cic.Calculate; 8 9 public class CalculateTest { 10 String p = "10000";//P:本金 11 String i = "0.030";//I:利率 12 String n = "30";//N:存入年限 13 String c = "1";//C: 年复利次数 14 String f = "0";//F:复利终值 15 16 Calculate calculate = new Calculate(p, i, n, c, f); 17 18 19 @Test 20 //测试求复利公式 21 public void testCalculateCompoundInterest() { 22 assertEquals(new Calculate(p, i, n, c, f).calculateCompoundInterest(), "24272.6247"); 23 } 24 @Test 25 //测试求单利公式 26 public void testCalculateSimpleInterest() { 27 assertEquals(new Calculate(p, i, n, c, f).calculateSimpleInterest(), "19000.0000"); 28 } 29 @Test 30 //测试求利率 31 public void testCalculateI() { 32 assertEquals(new Calculate("1000", null, "5", "1", "1200").calculateI(), ".0371"); 33 } 34 @Test 35 //测试求本金 36 public void testCalculatePrincipal() { 37 assertEquals(new Calculate(null, "0.03", "10", "1", "50000").calculatePrincipal(), "37204.6957"); 38 } 39 @Test 40 //测试求时间公式 41 public void testCalculateYears() { 42 assertEquals(new Calculate("30000", "0.03", null, "1", "50000").calculateYears(), "18"); 43 } 44 @Test 45 //测试求回报公式 46 public void testCalculateInvestment() { 47 assertEquals(new Calculate("20000", "0.025", "10", null, null).calculateInvestment(), "229669.3262"); 48 } 49 @Test 50 //测试求还款公式 51 public void testCalculateRepayment() { 52 assertEquals(new Calculate("1000000", "0.0325", "10", null, null).calculateRepayment(), "9771.9029"); 53 } 54 @Test 55 //测试输入非法字符 56 public void testCalculateinput() { 57 p = "adff"; 58 assertTrue(new Calculate(p, i, n, c, f).isError()); 59 } 60 @Test 61 public void testCalculateinput2() { 62 i = "1.2"; 63 assertTrue(new Calculate(p, i, n, c, f).isError()); 64 } 65 @Test 66 public void testCalculateinput3() { 67 n = "10.5"; 68 assertTrue(new Calculate(p, i, n, c, f).isError()); 69 } 70 @Test 71 public void testCalculateinput4() { 72 c = "1.5"; 73 assertTrue(new Calculate(p, i, n, c, f).isError()); 74 } 75 @Test 76 //测试输入为空 77 public void testCalculateCompoundInterest3() { 78 p = null; 79 assertTrue(new Calculate(p, i, n, c, f).getPrincipal()==0); 80 } 81 @Test 82 //测试字符串长度为0 83 public void testCalculateCompoundInterest4() { 84 p = ""; 85 assertTrue(new Calculate(p, i, n, c, f).getPrincipal()==0); 86 } 87 88 }
测试结果均符合预期
以上是关于复利计算器单元测试的主要内容,如果未能解决你的问题,请参考以下文章