复利计算器Junit单元测试

一。测试场景

测试模块 测试输入 预期结果 运行结果 bug跟踪
复利计算

(本金,利率,年限,次数)

终值

   

测试运算结果

(100,5,3,1) 115.76 115.76  
测试输入负数 (-100,5,3,1) False False  
测试输入0 (0,5,3,1) False False  
单利计算 (本金,利率,年限)

终值

   

测试运算结果

(1000,2,5) 1100 1100  
测试输入负数 (-1000,2,5) False False  
测试输入0 (0,2,5) False False  
本金估算 (终值,利率,年限,次数) 本金    

测试运算结果

(1000,2,5,1) 905.73 905.73  
测试输入负数 (-1000,2,5,1) False False  
测试输入0 (0,2,5,1) False False  
年限估算 (本金,利率,次数,终值) 年限    

测试运算结果

(1000,2,1,2000) 35 35  
测试输入负数 (-1000,-2,1,2000) False False  
测试输入0 (0,2,1,2000) False False  
利率估算 (本金,年限,次数,终值) 利率    

测试运算结果

(1000,5,1,2000) 14.86 14.86  
测试输入负数 (-1000,-5,1,2000) False False  
测试输入0 (0,0,1,2000) False False  
按年投资 (年投资额,利率,定投年数)      

测试运算结果

(1000,2,5) 5308.12 5308.12  
测试输入负数 (-1000,2,5) False False  
测试输入0 (0,2,5) False False  
按月投资 (月投资额,利率,定投月数)      

测试运算结果

(1000,2,6) 6035.09 6035.09  
测试输入负数 (-1000,2,6) False False  
测试输入0 (0,2,6) False False  
等额本息还款 (贷款金额,利率,年限,次数)      

测试运算结果

(10000,2,5,2) 175.16 175.16  
测试输入负数 (-10000,2,5,2) False False  
测试输入0 (0,2,5,2) False False  

二。测试代码

 import static org.junit.Assert.*;

 import org.junit.Assert;
import org.junit.Before;
import org.junit.Test; public class test {
@Before
public void setUp() throws Exception {
}
@org.junit.Test
public void testCompound() {
CompoundCalculator Compound = new CompoundCalculator();
double F = Compound.Compound(100,5,3,1);
Assert.assertEquals(F, 115.76, 1.0);
// assertTrue(F>0);
double f =Compound.Compound(-100,5,3,1);
assertFalse(f>0);
double a=Compound.Compound(0,5,3,1);
assertFalse(a>0);
}
@org.junit.Test
public void testSimple() {
CompoundCalculator Simple = new CompoundCalculator();
double F = Simple.Simple(1000,2,5);
Assert.assertEquals(F, 1100, 0.0);
// assertTrue(F>0);
double f =Simple.Simple(-1000,2,5);
assertFalse(f>0);
double a=Simple.Simple(0,2,5);
assertFalse(a>0);
}
@org.junit.Test
public void testPrinciple() {
CompoundCalculator Principle = new CompoundCalculator();
double F = Principle.Principle(1000,2,5,1);
Assert.assertEquals(F, 905.73, 1.0);
// assertTrue(F>0);
double f =Principle.Principle(-1000,2,5,1);
assertFalse(f>0);
double a=Principle.Principle(0,2,5,1);
assertFalse(a>0);
}
@org.junit.Test
public void testYear() {
CompoundCalculator Year = new CompoundCalculator();
double F = Year.Year(1000,2,1,2000);
Assert.assertEquals(F, 35, 0.0);
// assertTrue(F>0);
double f =Year.Year(-1000,-2,1,2000);
assertFalse(f>0);
double a=Year.Year(0,2,1,2000);
assertFalse(a<0);
}
@org.junit.Test
public void testRate() {
CompoundCalculator Rate = new CompoundCalculator();
double F = Rate.Rate(1000,5,1,2000);
Assert.assertEquals(F, 14.86, 1.0);
// assertTrue(F>0);
double f =Rate.Rate(-1000,-5,1,2000);
assertFalse(f>0);
double a=Rate.Rate(0,0,1,2000);
assertFalse(a<0);
}
@org.junit.Test
public void testYearinvest() {
CompoundCalculator Yearinvest = new CompoundCalculator();
double F = Yearinvest.Yearinvest(1000,2,5);
Assert.assertEquals(F, 5308.12, 1.0);
// assertTrue(F>0);
double f =Yearinvest.Yearinvest(-1000,2,5);
assertFalse(f>0);
double a=Yearinvest.Yearinvest(0,2,5);
assertFalse(a>0);
}
@org.junit.Test
public void testMonthinvest() {
CompoundCalculator Monthinvest = new CompoundCalculator();
double F = Monthinvest.Monthinvest(1000,2,6);
Assert.assertEquals(F, 6035.09, 1.0);
// assertTrue(F>0);
double f =Monthinvest.Monthinvest(-1000,2,6);
assertFalse(f>0);
double a=Monthinvest.Monthinvest(0,2,6);
assertFalse(a>0);
}
@org.junit.Test
public void testRepayment() {
CompoundCalculator Repayment = new CompoundCalculator();
double F = Repayment.Repayment(10000,2,5,2);
Assert.assertEquals(F, 175.16, 1.0);
// assertTrue(F>0);
double f =Repayment.Repayment(-10000,2,5,2);
assertFalse(f>0);
double a=Repayment.Repayment(0,2,5,2);
assertFalse(a>0);
} }

三。测试结果

复利计算器Junit单元测试

上一篇:【搜索引擎Jediael开发笔记】v0.1完整代码


下一篇:使用Junit等工具进行单元测试