复利计算4.0-单元测试
预期单元测试的场景:
1.输入非法无效字符;
2.测试计算公式
测试代码部分:
1 System.out.print("请输入本金:"); 2 double money = scanner.nextDouble(); 3 System.out.print("请输入存钱年数:"); 4 int years = scanner.nextInt(); 5 System.out.print("请输入利率:"); 6 double r = scanner.nextDouble(); 7 sum = money + money * years * r; 8 System.out.println("单利终值为:" + sum); 9 sum = 0;
Test方法代码:
@Test public void testInput0(){ Calculate5 a = new Calculate5(); boolean result = a.Input(1000000, 30, 0.03); Assert.assertEquals(true,result); } @Test public void testInput1(){ Calculate5 a = new Calculate5(); boolean result = a.Input(-1, 30, 0.03); Assert.assertEquals(false,result); } @Test public void testInput2(){ Calculate5 a = new Calculate5(); boolean result = a.Input(1000000, -1, 0.03); Assert.assertEquals(false,result); } @Test public void testInput3(){ Calculate5 a = new Calculate5(); boolean result = a.Input(1000000, 30, -0.1); Assert.assertEquals(false,result); } @Test public void testInput4(){ Calculate5 a = new Calculate5(); boolean result = a.Input(1000000, 30, 3); Assert.assertEquals(false,result); }
//计算公式
@Test
总结:
对于对于一些横跨代码较多的单元测试力不从心,没有将之前的需求计算等封装打包成类进行静态测试,这次只有简单的测了个输入输出,将继续在后面改进。